Django Basics

Mohith Aakash G
featurepreneur
Published in
3 min readJan 11, 2022

Django is a high level Python-based web development framework that is used in creating and maintaining efficient and quality websites. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database — SQLlite3, etc.

Why Django Framework?

  • Excellent documentation and high scalability.
  • Used by Top MNCs and Companies, such as Instagram, Disqus, Spotify, Youtube, Bitbucket, Dropbox, etc. and the list is never-ending.
  • Easiest Framework to learn, rapid development and Batteries fully included.
  • The last but not least reason to learn Django is Python, Python has huge library and features such as Web Scrapping, Machine Learning, Image Processing, Scientific Computing, etc. One can integrate it all this with web application and do lots and lots of advance stuff.

Installation of Django

To install django we use the pip command

pip install django

Starting a project

In Django, every web app you want to create is called a project and a project is a sum of applications.

Start a project by following command

django-admin startproject projectName

This creates a new Folder named projectName. Change current directory to projectName.

cd projectName

Now, to check our installation,

python manage.py runserver

Now visit http://localhost:8000/

If you can connect to the url and see a welcome page like this, then your django setup is successful!

Creating an App

Django is famous for its unique and fully managed app structure. For every functionality, an app can be created like a completely independent module.

To create a app, run the following command

python manage.py startapp projectApp

Now projectName has the following structure:

projectName/
manage.py
projectName/
__init__.py
settings.py
urls.py
wsgi.py
projectApp/
__init__.py
admin.py
models.py
tests.py
views.py

To consider the app in your project you need to specify your project name in INSTALLED_APPS list as follows in settings.py:

# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','projectApp']

Creating a View

Now that we’ve created an app we can start modifying some files within our app to create our first web page (aka view).

To do this we will go into our apps root directory and modify the file called views.py.

# views.py file
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world!")

Linking to our View

Now that we’ve created a view we need to create a url to link to it. To do this we need to create a new python file called urls.py. This file will be in root directory of our app (same place as views.py). It should contain the code shown below.

# urls.py
from django.urls import path
from . import viewsurlpatterns = [
path('', views.index, name='index'),
]

The last step is to exit this directory and go into the interior directory of our site name. Here we should see a file called urls.py (a different one that the one we just created). We need to modify it to be the following:

# urls.py 
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('projectApp.urls')),
path('admin/', admin.site.urls),
]

Now if we re run our server and visit the url, it would say Hello World!

--

--