Django Development for Beginner’s
This blog is for beginners that want to learn Django. Let’s start Django tutorials for beginners. Django is a Python-based web development framework that is used for web development. As Django is a Python-based web framework you must learn Python before starting with Django. Get familiar with data types and variables and conditional statements in Python.
The web development framework Django is written in Python. Django framework is a software that supports the development of dynamic Web sites, applications, and services. Django provides us with a set of tools and functionalities that solves many common problems associated with Web development. The common problems include security features, database access, sessions, template processing, URL routing, internationalization, localization, and many others.
When we use a web framework, such as Django, it enables us to develop secure and reliable Web applications very quickly in a standardized way.
Out of many web development frameworks, Django is one of the most popular Web frameworks written in Python. Django is definitely the most complete, offering a wide range of features out-of-the-box, such as a standalone Web server for development and testing, caching, middleware system, ORM, template engine, form processing, interface with Python’s unit testing tools.
For the development of Django, Django has the support of the Django Software Foundation, and Django is also sponsored by companies like JetBrains and Instagram. For more than 12 years, Web development framework Django is under active development now, proving to be a mature, reliable and secure Web framework.
To start learning the Django framework the first thing we need to do is install some programs on our machine. We need to install the basic setup having Python, Virtualenv, and Django.
Now using virtual environments is not mandatory, but it’s highly recommended.
For the development of Web sites or Web projects using Django, we will need to install external libraries to support the development. We have to use virtual environments so that each project you develop will have an isolated environment. So the dependencies won’t clash.
The first thing we want to do is install the latest Python version.
Go to www.python.org click on the Python download page.
Pick the right version accordingly to your Windows.
Once done with the download, go to your Downloads directory, right-click on the installer and click on Run as administrator.
To install virtualenv, we are going to use pip, a tool to manage and install Python packages.
Open the Command Prompt, and execute the command given below:
pip install virtualenv
First, let’s create a folder named Development on our personal computer, you can use any other name of your choice. Let’s start by creating a new folder with the project name. For now, we can call it myproject.
The folder we created above is the higher level directory that will store all the files and things related to our Django project, including its virtual environment.
Let’s create our very first virtual environment and installing Django.
Inside the myproject folder:
Execute the following command
Our virtual environment is created. Before we start using it, we need to activate:
If you see (venv) in front of the command line, then it worked for you.
To deactivate the venv run the command below:
It’s very straightforward. Now we have the venv activated. After this we need to run the following command to install Django:
pip install Django
Starting a New Project:
For Django application creation we need to start a new Django project. For this run the command is given below:
django-admin startproject myproject
Once we execute the command given above, it will generate the base folder structure for a Django project.
Right now, our myproject directory looks like this:
Django’s initial project structure is composed of five files:
manage.py: This file is a shortcut to use the django-admin command-line utility. This command is used to run management commands related to our project. We are going to use it to run the development server, run tests, create migrations and much more.
__init__.py: The __init__ empty file tells Python that this folder is a Python package.
settings.py: File settings.py contains all the configuration of the project.
urls.py: urls.py is responsible for mapping the routes and paths in our project. If you want to show something, then you have to map it here first.
wsgi.py: This is a simple gateway interface used for deployment.
As the Django comes with a simple web server installed. So we don’t need to install anything else to run the project locally. To run the server executing the command given below:
python manage.py runserver
Go to the browser and open URL in a Web browser: http://127.0.0.1:8000
Django is built on principle on “Do not repeat yourself”. To achieve reusability in Django and to keep everything separate apps are used. A separate app is created for each thing that can be identified separately for a web site.
In Django we have two important concepts:
app: In the Django app is a Web application that does something. An app is composed of a set of models (database tables), views, templates, tests.
project: is a collection of configurations and apps. In a single project, we can have multiple apps or a single app. We can’t run a Django app without a project. Simple websites can be written entirely inside a single app.
It’s a way to organize the source code. In the initial phase, it’s not very trivial to determine what is an app or what is not an app. How to organize the code and so on. But don’t worry much about that right now! First of all let’s get comfortable with Django’s API and the fundamentals.
Let’s create a simple example. For the creation of our first app, go to the directory where the manage.py file is and executes the following command:
django-admin startapp boards
Note that we used the command startapp this time.
Once we have created our first app, let’s configure our project to use it.
For configuration of the project, open the settings.py and try to find the INSTALLED_APPS variable:
INSTALLED_APPS = [
In this blog, we will further explore those apps as we progress in this tutorial series. For now, let them be. Just add our boards app(that we created recently) to the list of INSTALLED_APPS as specified below:
INSTALLED_APPS = [
To specify the presentation logic views.py file is used. Let’s write our first view. As this is our first app, let’s just experiment how it looks like to create a new page with Django.
We have to write the code inside views.py file, for this open the views.py file inside the boards app, and add the following code:
from django.http import HttpResponse
return HttpResponse(‘Hello, World!’)
The function views are Python functions that receive an HttpRequest object and returns an HttpResponse object. It will Receive a request as a parameter and returns a response as a result. This will be the flow of our project that you have to keep in mind!
Thus, here we have defined a simple view function called home which simply returns a message saying Hello, World!.
Once the function is defined, we have to tell Django when to serve this view. This is done inside the urls.py file of project(We can also create URLs at the app level, will see it later):
from django.conf.urls import url
from django.contrib import admin
from boards import views
urlpatterns = [
path(‘home/’, views.home, name=’home’),
You can compare the snippet given above with your urls.py file in your project, you will notice that I have added the following new line: path(‘home/’, views.home, name=’home’). As the home function is defined in views.py thus also imported the views module from our app boards using the command: from boards import views.
As per the path specified Django works with the path to match the requested URL. We can also use a regular expression. To use a regular expression we need to use re_path instead of the path. I am using the function home from views.py file., I’m using the ”, which will match a home path, which is the homepage (this url: http://127.0.0.1:8000). If I wanted to match the URL http://127.0.0.1:8000/homepage/, my url would be a path(‘homepage’, views.home, name=’home’).
To see the result, let’s run the server first. To run the server use the following command
python manage.py runserver
Once you execute this command it will display the home page url if there are no errors in our project. If there is any error then it will be shown in command prompt. After successfully starting the server. Go to the web browser and open the http://127.0.0.1:8000 URL:
You can see that it has printed “Hello World” onto the web browser.
In this blog, we have learned how to install the latest Python version and how to set up the development environment. Also we had an introduction to virtual environments and started our very first Django project and already created our first app.
Nikita Jagtap | SevenMentor Pvt. Ltd.
Call the Trainer and Book your free demo Class for Now!!!
© Copyright 2020 | Sevenmentor Pvt Ltd.