A Small Introduction to Django

Samhith Vasikarla
4 min readJul 3, 2021

--

Installation of django:

pip3 install django

To create a project in Django named ‘Hello’:

Command: django-admin startproject Hello

With the above command we create a project named Hello and some inbuilt code is given to the user so that user can do modifications of his need

To get to know whether the django is working properly

type command : python manage.py runserver inside the project we created

Django server running successfully

Model View Template Architecture of Django:

Model: It is the information used in database design.

View: runtime we pull the data from model and do some calculation on the data. Gives the processed output to a template

Create an app in django:

There could be a problem in understanding what is a project and what is app? To become a project fully functional there could be at least one or more apps. In simple projects there could be only one app

Command to create an app

python manage.py startapp <App name>

Here consider the app name is HelloWorldApp

URL Dispatching :

Before understanding how the django works, we put a new file called urls.py in app (HelloWorldApp)

whenever we want to access a URL in the project. The controller goes to the urls.py in the project page

“””HelloWorld URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:

https://docs.djangoproject.com/en/3.2/topics/http/urls/

Examples:

Function views

1. Add an import: from my_app import views

2. Add a URL to urlpatterns: path(‘’, views.home, name=’home’)

Class-based views

1. Add an import: from other_app.views import Home

2. Add a URL to urlpatterns: path(‘’, Home.as_view(), name=’home’)

Including another URLconf

1. Import the include() function: from django.urls import include, path

2. Add a URL to urlpatterns: path(‘blog/’, include(‘blog.urls’))

from django.contrib import admin

from django.urls import path,include

urlpatterns = [

path(‘admin/’, admin.site.urls),

path(‘’,include(‘HelloWorldApp.urls’))

]

This is how urls.py page looks like in project page. It tells that if the url is ending with admin go to the admin.site.urls

If nothing is given in the URL then go to the URL page of HelloWorldApp

from django.contrib import admin

from django.urls import path

from HelloWorldApp import views

urlpatterns = [

path(‘’,views.index,name=’Home’)

]

This is how urls.py page looks in the app page.This states that if the url is blank then go to the views.py and call the function index

from django.shortcuts import render,HttpResponse

# Create your views here.

def index(request):

context={

‘variable’: ‘this is sent from views’

}

return render(request,’index.html’,context)

#return HttpResponse(‘This is a home page’)

def about(request):

return render(request,’about.html’)

# return HttpResponse(‘This is a about page’)

def services(request):

return render(request,’services.html’,)

#return HttpResponse(‘This is a services page’)

def contact(request):

return render(request,’contact.html’)

#return HttpResponse(‘This is a contact page’)

This is the views.py page. If a user calls with out any name then index function gets executed.

Generally we do not like to see webpage stating ‘ This is home page’. All want beautiful web pages. So instead of text we use templates and render them

Static files:

Static files are the files that are publicly accessible to the user.

Configuration of static files:

1. Create a folder called static inside the project.

2.Inside the folder create a file (named staticfile.txt). Add the contents you wish to add

3. In your settings file which is present in the Project folder ,

define STATIC_URL, for example:

STATIC_URL = ‘/static/’

3. Add the below code in settings.py

STATICFILES_DIRS=[

os.path.join(BASE_DIR,’static’)

]

Note: Do not put sensitive data in the static files as any user can see the data and become a security loop hole

Templates:

These are normal html files which are used to display nice webpages to the user

Configuration of templates.

1. Create a folder called templates inside the project

1. In settings.py we have

TEMPLATES = [

{

‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,

‘DIRS’: [os.path.join(BASE_DIR,’templates’)],

‘APP_DIRS’: True,

‘OPTIONS’: {

‘context_processors’: [

‘django.template.context_processors.debug’,

‘django.template.context_processors.request’,

‘django.contrib.auth.context_processors.auth’,

‘django.contrib.messages.context_processors.messages’,

],

},

},

]

In DIRS we place the template folder name.

Inside the template folder add the html files as much as you want. In this example I added only one html file index.html

To see the html page we use app.views page and their instead of return HTTPResponse we use return render(request,’<name of the html page >’)

def index(request):

return render(request,’index.html’)

python manage.py makemigrations — — sees whether there is any database changes ocured with in the the project

Create super user in Django:

Super-user is the administrator of the particular project.

To create superuser

python manage.py createsuperuser

If any error comes likes the below:

sqlite3.OperationalError: no such table: auth_user

The above exception was the direct cause of the following exception:………………….

then do the

python manage.py makemigrations

python manage.py migrate

python manage.py createsuperuser

After successful creation of superuser goto 127.0.0.1:8000/admin and enter the credentials if the createsuperuser is success then login to the administrator

Maintain same theme in all the web pages.

It is important in production that all the webpages have same style that means the style present in Home.html should be same as style in contacts.html

To achieve that we put all the style in the base.html file in the templates folder.

And in each html page like Home.html the first statement would be like

{% extends ‘base.html’ %}

when django sees this line it extends the properties present in the base.html and renders to the Home.html page

Furthermore, we will discuss in the next blog

--

--

No responses yet