What is Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.Django was designed to help developers take applications from concept to completion as quickly as possible, takes security seriously and helps developers avoid many common security mistakes and exceedingly scalable.
Some well known sites that use Django include PBS, Instagram, Disqus, Washington Times, Bitbucket, YouTube, Gmail and Mozilla.
Getting Started with Django
Django use Model View Template which follows the Model View Controller pattern. The Model helps to handle database.The Template is a presentation layer which handles User Interface part completely. The View is used to execute the business logic and interact with a model to carry data and renders a template.
Requirements for this Tutorial
- Have Python installed: Install Python
- Have a code editor installed: I use Visual Studio Code
Let's create a directory for a clean and well structured arrangement.
$ mkdir webapp
cd into the project with:
Set Up Your Virtual Environment
Next thing we need to do, is set up our virtual environment, a virtual environment helps you run several versions of python/django right on your machine.AS you could have two different python/django projects running on different django versions without clashing.
To set up our virtual environment, we’ll be using python’s package manager
pip to do the installation.
$ pip install virtualenv
After installation,it’s time to create a virtual environment that would enable us use a preferred django version of our choice.
$ virtualenv env
Let's now open our VS code by:
Activating Virtual Environment in VS code click on terminal at the top navigation section then select new terminal
Install Django version 3.2 in the terminal:
$ pip install django==3.2
Starting a project
At the moment we have django in our virtual environment, it's time to start up our first project.In our terminal type in:
django-admin.py startproject web .
this creates a folder
manage.py in our webapp directory .
Django gives us files that makes our work easily.
__init__.pyfile - makes Python treat directories containing it as modules.
asgi.pyfile - ASGI (Asynchronous Server Gateway Interface) provides an interface between async Python web servers and applications while it supports all the features provided by WSGI
settings.pyfile - is a core file in Django projects. It holds all the configuration values that your web app needs to work; database settings and a bunch other stuff.
urls.py- this file returns an element for inclusion in urlpatterns.
wsgi.py- this file is Django's primary deployment platform platform.
manage.py- thii file is a command-line utility that works similar to the django-admin command.
Make migrations to our database
Migrations helps us make changes to our database schema without losing any data, each time we create a new model or make changes to a current one and run migrations, it helps update our database tables with the schemas without having to go through all the stress of dragging and recreating the database ourselves.
To make migrations to database type in:
python manage.py migrate
This outputs the following:
This is a proof that our migrations have been made to our database.
Running our server
Fire up our server using the manage.py file in our project.
python manage.py runserver
Now we have our server running on
http://127.0.0.1:8000/, on webpage this is what we get to show our first project is running.
Create our first App in our project.
We will now create a app named
app .To create the app, run the following command:
$ python manage.py startapp app
This will create another directory called
app with files.
__init__.pytells Python to treat the directory as a Python package.
admin.pycontains settings for the Django admin pages.
apps.pycontains settings for the application configuration.
models.pycontains a series of classes that Django’s ORM converts to database tables.
tests.pycontains test classes.
views.pycontains functions and classes that handle what data is displayed in the HTML templates.
Once the app is created, install it in our project in
webapp/web/settings.py add this line in
This identifies the project that the app created exist.
*Add super user *
To add superuser in our django project run the following code in the terminal.
python manage.py createsuperuser
Fill the username, email, and password
Login to the our django admin with
http://127.0.0.1:8000/admin and make sure your server is running.
Create our first template
Create a directory named
templates in the app directory.
Create a View
Navigate to the
webapp/app/views.py and create a view function.
We have added a file home.html which we haven't created. To do so add a create a home.html file named in our template directory.
You’ve now created a function to handle your views and templates html file to display to the user.
Urls.py file in app directory to hook up our URLs to be able to navigate to the web page we just created.In
web/urls.py include a URL configuration for the
app. Inside the web/urls.py include the following lines:
from django.urls import path, **include**
This checks for the module urls.py in app. The module doesn't exist yet, so we need to create if :
In this module created we need to import the
path object and our
Let's add some lines of code in our
Now, when you restart the server and visit
http://127.0.0.1:8000, you should be able to see the HTML template you created.
Add a link to admin page in the html page:
Add Bootstrap to our App
We will use bootstrap for styling to our project rather than going the CSS styling .To install bootstrap in your app , use the Bootstrap CDN. This is an easier way to install Bootstrap that involves just adding a few lines of to our
Now, when you visit
http://127.0.0.1:8000, you should see that the page has been formatted with slightly different styling.
In this section we have created our first django App and able to see our web page. Check out the source code
Top comments (4)
Good one for a start. thanks for sharing this.
Thanks for your feedback
Are you going to finish writing this?