Python Django
Python Django
Crush course
Django Framework
■ 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
■ 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