Brief notes of Django
Introduction
Invented in 2003, Django which is a Python-
based server side web framework which is free
and open source follows MVT (Model View
Template) design pattern.
Usually, model is written in [Link] and view
in [Link]. Templates are made available in
‘templates’ folder.
It also has ready-to-use features like login
system, database connection and CRUD
operations.
Installation of Django
In order to install Django, it is essential to first
install Python (download from
[Link]
Then install Djano by running following
command in command prompt
python –m pip install Django
First project in Django
In order to create a project in Django, following
below steps
1. Create a new folder. Eg: ‘college’
2. Open the same folder in VS code.
3. Run following command to create a
project in the folder
django-admin startproject college
4. In order to run the website, following
command need to be executed in terminal of
VS code itself.
python [Link] runserver
Osmania University website in Django
1. Create a folder by name ‘django’.
2. Open the above folder in VS code.
3. Run following command in VS code by
opening terminal
django-admin startproject OU
4. Then run command ‘cd OU’ to change
the directory.
5. To run the server, execute the following
command.
python [Link] runserver
6. To see output, visit url
[Link]
7. Create app by running following
command
python [Link] startapp results
8. Open [Link] under ‘OU’ app, and write
following code.
from [Link] import admin
from [Link] import path, include
urlpatterns = [
path(' ', include('[Link]')),
path('admin/', [Link]),
]
9. Create [Link] under ‘results’ app, write
following code.
from [Link] import path
from . import views
urlpatterns = [
path(' ', [Link])
]
10. Open [Link] under ‘results’ app and
write following code.
from [Link] import render
def index(request):
return render(request,'[Link]')
def marks(request):
return render(request,'[Link]')
11. Create two folders under main ‘OU’
website.
[Link]
b. static
12. Deploy all HTML files in ‘templates’
folder and other static files and folders in
‘static’ folder.
13. Edit ‘[Link]’ and add an item
‘templates’ to ‘DIR’ list.
14. To load static files, following below
steps
[Link] following code in HTML files
{% load static %}
{% static "images" as imageURL %}
b. Open [Link] under ‘results’ app and
write following code
from [Link] import path
from .import views
from [Link] import settings
from [Link]
import static
urlpatterns = [
path('', [Link]),
path('marks', [Link]),
]
urlpatterns = urlpatterns +
static(settings.MEDIA_URL,
document_root =
settings.MEDIA_ROOT)
[Link] [Link] under ‘OU’ app and add
following code at the end of the file
STATIC_URL = '/static/'
STATICFILES_DIRS = [
[Link](BASE_DIR,'static')
]
STATIC_ROOT =
[Link](BASE_DIR,'assets')
MEDIA_URL = '/media/'
MEDIA_ROOT =
[Link](BASE_DIR,'media')
d. Write below code at the beginning of
[Link]
import os
[Link] template tags. Eg: {% static
'[Link]' %}
15. Under ‘results’ app, open [Link] and
write following code.
from [Link] import models
class Result([Link]):
name= [Link]()
current_date = [Link](auto_now=True)
class Marks([Link]):
name= [Link](max_length=255)
htno= [Link](max_length=255)
subject1= [Link]()
subject2= [Link]()
subject3= [Link]()
subject4= [Link]()
subject5= [Link]()
subject6= [Link]()
16. Register the models. Open [Link]
under ‘results’ app and write following code
from [Link] import admin
from .models import Result, Marks
[Link](Result)
[Link](Marks)
17. Add '[Link]' to
'INSTALLED_APPS' under the [Link].
18. To migrate Database from [Link] to
database, execute following command
python [Link] makemigrations
python [Link] migrate
19. Create superuser by following command
python [Link] createsuperuser
20. Open admin panel by visiting following
URL
[Link] and enter
username and password set in the last step.
21. Open [Link] under ‘results’ app and
write following code.
from [Link] import render
from .models import Result, Marks
def index(request):
list_of_results = [Link]()
return render(request,'[Link]',
{'list_of_results':list_of_results})
22. Write following code in [Link] to
load the results from the database on the
homepage.
{% for element in list_of_results %}
{%endfor%}
23. Open ‘[Link]’ under ‘results’ app and
add following path in the urlspatterns.
path('validate', [Link])
24. def validate(request):
htno = [Link]['htno']
try:
dataMarks =
[Link](htno=htno)
return
render(request,'[Link]',
{'dataMarks':dataMarks})
except [Link]:
return render(request,'[Link]',
{‘htno’:htno})
25. Create [Link] to display error above
the form. Add ‘csrf_token’ in [Link]
under templates folder.