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 models.py and view
in views.py. 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
https://www.python.org/downloads/).
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 manage.py 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 manage.py runserver
6. To see output, visit url
http://127.0.0.1:8000/
7. Create app by running following
command
python manage.py startapp results
8. Open urls.py under ‘OU’ app, and write
following code.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(' ', include('results.urls')),
path('admin/', admin.site.urls),
]
9. Create urls.py under ‘results’ app, write
following code.
from django.urls import path
from . import views
urlpatterns = [
path(' ', views.index)
]
10. Open views.py under ‘results’ app and
write following code.
from django.shortcuts import render
def index(request):
return render(request,'index.html')
def marks(request):
return render(request,'result.html')
11. Create two folders under main ‘OU’
website.
a.templates
b. static
12. Deploy all HTML files in ‘templates’
folder and other static files and folders in
‘static’ folder.
13. Edit ‘settings.py’ and add an item
‘templates’ to ‘DIR’ list.
14. To load static files, following below
steps
a.Add following code in HTML files
{% load static %}
{% static "images" as imageURL %}
b. Open urls.py under ‘results’ app and
write following code
from django.urls import path
from .import views
from django.conf import settings
from django.conf.urls.static
import static
urlpatterns = [
path('', views.index),
path('marks', views.marks),
]
urlpatterns = urlpatterns +
static(settings.MEDIA_URL,
document_root =
settings.MEDIA_ROOT)
c.Open setting.py under ‘OU’ app and add
following code at the end of the file
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT =
os.path.join(BASE_DIR,'assets')
MEDIA_URL = '/media/'
MEDIA_ROOT =
os.path.join(BASE_DIR,'media')
d. Write below code at the beginning of
setting.py
import os
e.Add template tags. Eg: {% static
'style.css' %}
15. Under ‘results’ app, open models.py and
write following code.
from django.db import models
class Result(models.Model):
name= models.IntegerField()
current_date = models.DateField(auto_now=True)
class Marks(models.Model):
name= models.CharField(max_length=255)
htno= models.CharField(max_length=255)
subject1= models.IntegerField()
subject2= models.IntegerField()
subject3= models.IntegerField()
subject4= models.IntegerField()
subject5= models.IntegerField()
subject6= models.IntegerField()
16. Register the models. Open admin.py
under ‘results’ app and write following code
from django.contrib import admin
from .models import Result, Marks
admin.site.register(Result)
admin.site.register(Marks)
17. Add 'results.apps.ResultsConfig' to
'INSTALLED_APPS' under the setting.py.
18. To migrate Database from model.py to
database, execute following command
python manage.py makemigrations
python manage.py migrate
19. Create superuser by following command
python manage.py createsuperuser
20. Open admin panel by visiting following
URL
http://127.0.0.1:8000/admin/ and enter
username and password set in the last step.
21. Open views.py under ‘results’ app and
write following code.
from django.shortcuts import render
from .models import Result, Marks
def index(request):
list_of_results = Result.objects.all()
return render(request,'index.html',
{'list_of_results':list_of_results})
22. Write following code in index.html to
load the results from the database on the
homepage.
{% for element in list_of_results %}
{%endfor%}
23. Open ‘urls.py’ under ‘results’ app and
add following path in the urlspatterns.
path('validate', views.validate)
24. def validate(request):
htno = request.POST['htno']
try:
dataMarks =
Marks.objects.get(htno=htno)
return
render(request,'marks.html',
{'dataMarks':dataMarks})
except Marks.DoesNotExist:
return render(request,'error.html',
{‘htno’:htno})
25. Create error.html to display error above
the form. Add ‘csrf_token’ in results.html
under templates folder.