Python-based web framework used for rapid development of web applications.
pip install django
The below command creates a new project named projectName
django-admin startproject projectName
The below command starts the development server.
python manage.py runserver
Django follows MVT(Model, View, Template) architecture.
The model represents the schema of the database.
from django.db import models
class Product(models.Model): # Product is the name of our model
product_id=models.AutoField
View decides what data gets delivered to the template.
from django.http import HttpResponse
def index(request):
return HttpResponse("Django CodeWithHarry Cheatsheet")
A sample .html file that contains 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>CodeWithHarry Cheatsheet</title>
</head>
<body>
<h1>This is a sample template file.</h1>
</body>
</html>
A python function that takes a web request and returns a web response.
from django.http import HttpResponse
def index(request):
return HttpResponse("This is a function based view.")
Django's class-based views provide an object-oriented way of organizing your view code.
from django.views import View
class SimpleClassBasedView(View):
def get(self, request):
pass # Code to process a GET request
Set of URL patterns to be matched against the requested URL.
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'),
]
from django.urls import include, path
urlpatterns = [
# ... snip ...
path('community/', include('aggregator.urls')),
path('contact/', include('contact.urls')),
# ... snip ...
]
Similar to HTML forms but are created by Django using the form field.
from django import forms
# creating a form
class SampleForm(forms.Form):
name = forms.CharField()
description = forms.CharField()
Apps in Django are like independent modules for different functionalities.
To create an app, use the following command:
python manage.py startapp AppName
After creating an app, you need to list the app name in the INSTALLED_APPS section of your settings.py file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AppName'
]
Used to handle dynamic HTML files separately.
To configure templates in your settings.py, you can use the following configuration:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
'APP_DIRS': True,
'OPTIONS': {
# some options here
},
},
]
A view is associated with every URL. This view is responsible for displaying the content from the template.
def index(request):
return render(request, 'index.html') # render is used to return the template
<!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 Django's way of updating the database schema according to the changes that you make to your models.
To create a migration, use the following command:
python manage.py makemigrations
This command is used to generate migration files with information on how to update the database schema, but it doesn't make changes to the actual database.
To apply the changes to the actual database, use the following command:
python manage.py migrate
This command is used to execute the pending migrations and update the database schema according to the changes defined in the migration files.
Django comes with a ready-to-use admin interface.
To create an admin user, use the following command:
python manage.py createsuperuser
This command is used to create a superuser account with administrative privileges, allowing you to access and manage the Django admin interface.
Redirection is used to redirect the user to a specific page of the application on the occurrence of an event.
from django.shortcuts import render, redirect
def redirecting(request):
return redirect("https://www.google.com")