Introduction to Django

Spread the love

Django is a python framework that lets you quickly and easily build secure data driven websites.

Django uses the Model-View-Template (MVT) architecture, which has three components:

  • Models are used for managing the data.
  • View handle the application logic , they fetch data from the models and display the results in the templates.
  • Template handles the presentation of data in a particular format as specified by the View.
Installation

Installing django is easy using pip which is a package management solution.

You will need python installed on your system, To install Django, we recommend that your first create a virtual environment and then run the command

pip install django

Django is now installed, now you need to create an project, you can create an project using the following command

django-admin.py startproject PROJECT_NAME

Which will create a  folder named PROJECT_NAME with files inside it belonging to your project, now you need to create an app, a Django project can use one or more apps. Create the app by first changing directory into the newly created project directory, then run the following command

python manage.py startapp app_name
Web App

We need to connect our app to the project, we do this by modifying the settings.py file present in the Project folder

In the settings.py file add your app name to the INSTALLED_APPS list

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_name'
]

next inside the apps folder, create a folder called “templates”, this will be where you store your app template files.

Next we need to connect the urls of the app to the project urls, to do this open the urls.py file in the app directory and add the following contents

from django.conf.urls import url
from . import views

urlpatterns = [     
url(r'^$', views.home, name='home'),
]

the urlpatterns is a list of urls, each url consists of 3 parts

r’^$’ is the regular expression used to call the view function when the url matches the given regular expression

views.home is the views function that will be called for this url

name=’home’  you can also optionally provide a name for your url

Next in the urls.py file in the project directory add the following import statement

from django.conf.urls import include

Next add the following url to the urlpatterns list

url(r'', include('app.urls')),

The above code matches the url with the regular expression, if it matches, the includes function is used to include the app urls.py file to handle the request.

Next we need to add some logic to the code, for this we add code in the views.py file. This is where the logic of the app resides.

def home(request):
return render(request, 'home.html', {})

In the above block of code we create a function called home, which takes in the request object, it then returns the home.html page 

Next create a directory called templates inside the app directory and place a html file called “home.html” inside it, write any html content within the file.

Now in the terminal or command prompt run the command

python manage.py runserver

The above command will start a webserver and if you open the url localhost:8000 in a browser , Django will get the url, see that it matches the “home” url pattern, it will then call the home view function, the function will then return the home.html file to the browser which you can view.

Django can do a lot more than what is mentioned in this post, If you would like to learn more about Django, then the official documentation is a good place to start.