What Is Django?: Creating A Project
What Is Django?: Creating A Project
What is Django?
Python-based web framework used for rapid development.
Creating a project
Starting a server
Django MVT
Django follows MVT(Model, View, Template) architecture.
Sample views.py
1/6
Home - CodeWithHarry
def index(request):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CodeWithHarry Cheatsheet</title>
</head>
<body>
</body>
</html>
Views in Django
Sample Function-Based Views
A python function that takes a web request and returns a web response.
def index(request):
Django's class-based views provide an object-oriented (OO) way of organizing your view code.
class SimpleClassBasedView(View):
2/6
Home - CodeWithHarry
URLs in Django
set of URL patterns to be matched against the requested URL.
urlpatterns = [
path('admin/', admin.site.urls),
Forms in Django
Similar to HTML forms but are created by Django using the form field.
# creating a form
class SampleForm(forms.Form):
Name = forms.CharField()
description = forms.CharField()
Apps in Django
Apps in Django are like independent modules for different functionalities.
Creating an app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AppName'
Templates in Django
Used to handle dynamic HTML files separately.
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
'APP_DIRS': True,
'OPTIONS': {
},
},
A view is associated with every template file. This view is responsible for displaying the content
from the template.
def index(request):
<!DOCTYPE html>
<html lang="en">
4/6
Home - CodeWithHarry
<head>
<meta charset="UTF-8">
<title>Template is working</title>
</head>
<body>
</body>
</html>
Migrations in Django
Migrations are Django's way of updating the database schema according to the changes that
you make to your models.
Creating a migration
The below command is used to make migration but no changes are made to the actual database.
The below command is used to apply the changes to the actual database.
Page Redirection
Redirection is used to redirect the user to a specific page of the application on the occurrence of
an event.
Redirect method
5/6
Home - CodeWithHarry
def redirecting(request):
return redirect("https://www.codewithharry.com")
6/6