Django is a Python-based web framework designed for rapid development of web applications.
Install Django using pip:
pip install djangoCreate a new project:
django-admin startproject projectNameStart the development server:
python manage.py runserverDjango follows the Model-View-Template (MVT) architecture.
Define a model representing the database schema:
from django.db import models
class Product(models.Model):
product_id = models.AutoFieldViews decide what data gets delivered to the template:
from django.http import HttpResponse
def index(request):
return HttpResponse("Django CodesWithpankaj Cheatsheet")A sample HTML file containing HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CodesWithpankaj Cheatsheet</title>
</head>
<body>
<h1>This is a sample template file.</h1>
</body>
</html>A function-based view:
from django.http import HttpResponse
def index(request):
return HttpResponse("This is a function based view.")A class-based view:
from django.views import View
class SimpleClassBasedView(View):
def get(self, request):
pass # Code to process a GET requestDefine URL patterns to be matched against the requested URL.
File 1:
from django.contrib import admin
from django.urls import path
from . import views
urlPatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
]File 2:
from django.urls import include, path
urlpatterns = [
# ... snip ...
path('community/', include('aggregator.urls')),
path('contact/', include('contact.urls')),
# ... snip ...
]Forms are created by Django using the form field.
from django import forms
class SampleForm(forms.Form):
name = forms.CharField()
description = forms.CharField()Apps are like independent modules for different functionalities.
Create a new app:
python manage.py startapp AppNameAdd the app name to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AppName'
]Templates handle dynamic HTML files separately.
Configure templates in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
'APP_DIRS': True,
'OPTIONS': {
# some options here
},
},
]Update the view to use a template:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Template is working</title>
</head>
<body>
<h1>This is a sample Django template.</h1>
</body>
</html>Migrations are used to update the database schema based on model changes.
Create migration files:
python manage.py makemigrationsApply changes to the actual database:
python manage.py migrateDjango provides a ready-to-use admin interface.
Create an admin user:
python manage.py createsuperuserRedirect users to a specific page on an event.
from django.shortcuts import render, redirect
def redirecting(request):
return redirect("https://www.codesWithpankaj.com")Feel free to explore more features and functionalities of Django in the official documentation: Django Documentation.