0% found this document useful (0 votes)
16 views17 pages

Python Django

Django is a Python web framework that encourages rapid development with reusable components and convention over configuration. It follows the MVT architectural pattern. The main components of Django include models, views, templates, URLs, forms, admin interface, and middleware. To install Django, use pip and start a new project with django-admin. The document then provides steps to create a simple portfolio project with Django, including defining a model, generating migrations, creating views and forms, and setting up URLs.

Uploaded by

josephboyong542
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
16 views17 pages

Python Django

Django is a Python web framework that encourages rapid development with reusable components and convention over configuration. It follows the MVT architectural pattern. The main components of Django include models, views, templates, URLs, forms, admin interface, and middleware. To install Django, use pip and start a new project with django-admin. The document then provides steps to create a simple portfolio project with Django, including defining a model, generating migrations, creating views and forms, and setting up URLs.

Uploaded by

josephboyong542
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 17

PYTHON DJANGO

Crush course
Django Framework

APIS FRONTEND FULLSTAC


K
What is Django?

■ Django is a high-level Python web framework that encourages


rapid development and clean, pragmatic design. It's designed to
help developers build web applications quickly, with less code
and a focus on reusability, maintainability, and scalability.
Django follows the Model-View-Controller (MVC)
architectural pattern, but in Django, it's referred to as Model-
View-Template (MVT).
Django Cores

■ Models: Models are Python classes that represent the data structure of your
application. Each model class corresponds to a database table, and the attributes
of the class represent fields in the table. Django provides an Object-Relational
Mapping (ORM) system that allows you to interact with the database using Python
code rather than SQL.
■ Views: Views are Python functions or classes that handle requests and return
responses. They encapsulate the logic of your application and determine what
content to display to the user.
■ Templates: Templates are HTML files with embedded Django template language.
They define the layout and structure of the user interface. Django templates allow
you to dynamically generate HTML content based on data from your views.
■ URLs: URLs are defined in the URLconf (URL configuration) file. The URLconf
maps URL patterns to view functions or classes. When a request is made to a
specific URL, Django uses the URLconf to determine which view to call.
■ Forms: Django provides a forms library that simplifies the process of handling
HTML forms and form data. Forms can be used to validate user input and interact
with models.
■ Admin: Django comes with a built-in administration interface that allows you to
manage your site's data without writing custom code. The admin interface is
automatically generated based on your model definitions.
■ Middleware: Middleware is a framework of hooks into Django's request/response
processing. It's a lightweight, low-level plugin system that modifies requests or
responses globally or based on certain conditions.
■ Authentication and Authorization: Django provides built-in support for user
authentication, permissions, and session management. This allows you to easily add
user authentication and authorization to your application.

■ Django's philosophy revolves around the principle of DRY (Don't Repeat Yourself) and
encourages rapid development through reusable components and convention over
configuration. It's widely used in the industry for building all sorts of web applications,
from small personal projects to large-scale enterprise systems.
Installation Procedures
■ $ pip install Django
■ $ django-admin –version
■ $ django-admin startproject
project_name
■ $ cd project_name
■ $ python manage.py runserver
Creating our Portifolio
1.$ python -m venv venv
2.$ venv\Scripts\activate #windows
3.$ source env/bin/activate #Linux
4.$ django-admin startproject
myportfolio
5.python manage.py startapp portfolio
Models
■ # portfolio/models.py
■ from django.db import models

■ class Contact(models.Model):
■ name = models.CharField(max_length=100)
■ email = models.EmailField()
■ message = models.TextField()
■ created_at = models.DateTimeField(auto_now_add=True)

■ def __str__(self):
■ return self.name
Making Migrations

■ $ python manage.py makemigrations


■ $ python manage.py migrate
■ # portfolio/views.py
■ from django.shortcuts import render, redirect
■ from .forms import ContactForm
■ from .models import Contact

■ def home(request):
■ return render(request, 'portfolio/home.html')

■ def contact(request):
■ if request.method == 'POST':
■ form = ContactForm(request.POST)
■ if form.is_valid():
■ form.save()
■ return redirect('home')
■ else:
■ form = ContactForm()
■ # portfolio/forms.py
■ from django import forms
■ from .models import Contact

■ class ContactForm(forms.ModelForm):
■ class Meta:
■ model = Contact
■ fields = ['name', 'email', 'message']
■ # portfolio/urls.py
■ from django.urls import path
■ from . import views

■ urlpatterns = [
■ path('', views.home, name='home'),
■ path('contact/', views.contact, name='contact'),
■ ]
■ $ python manage.py runserver
■ # myportfolio/settings.py

■ INSTALLED_APPS = [
■ ...
■ 'django.contrib.staticfiles',
■ ]

■ STATIC_URL = '/static/'
■ $env:TF_ENABLE_ONEDNN_OPT
S=0

You might also like