Hello pals, In this blog tutorial, I am guiding you to create a Django web application from stratch.

Setup Virtual Environment

Like every other Python Developers, we start a project by creating a virtual environment at first. A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. So, create a virtual environment on your system. If you don’t have it installed, install from PyPi: pip install virtualenv

Now, create the virtualenv like: virtualenv VIRTUAL_ENV_NAME Here, I’m naming it .env for sake of simplicity: virtualenv .env Now, activate the virtual environment you’ve created: source .env/bin/activate

Install Django Library from pip

Now, install the Django using the pip : pip install Django

Then, create a workspace folder to store the source code while you’re writing this guide. Here, I’m creating a directory named django_workspace. mkdir django_workspace

And change your current working directory to django_workspace. cd django_workspace

Create Django Project

Now, create the Django project here with the command : django-admin startproject YOUR_PROJECT_NAME .

The main objective of the project I’m gonna create is to fetch the weather data from public API of various cities and display on web. So, let’s name it weather_project . django-admin startproject weather_project

Django automatically generates a new project for us. We can visualize the file structure of the directory django_workspace which looks like :

django_workspace
├── manage.py
└── weather_project
    ├── asgi.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

We notice that, manage.py gets created at the root of the project folder. Every Django project starts with a manage.py file in its root. It’s a convenience script that allows you to run administrative tasks . Using the manage.py, I’m creating a app inside the project named weather which will contain the logic that our project carries.

Django Project Structure

The files have the following roles:

  • asgi.pyis a spiritual successor to wsgi, intended to provide a standard interface between async-capable Python web servers, frameworks, and applications.
  • __init__.py is a Python way to treat a directory as a package; it is empty.
  • settings.py contains all the configuration for our project.
  • urls.py controls the top-level URL routes.
  • wsgi.py stands for web server gateway interface and helps Django serve the eventual web pages.
  • manage.py executes various Django commands such as running the local web server or creating a new app.

Open the command prompt and run the Django Development Server using the following command: python manage.py runserver

This may result something like this:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 19, 2020 - 16:20:04
Django version 3.0.5, using settings 'weather_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Apply Migrations

We have a message regarding the unapplied migrations. Run migrate to sync the database with Django’s default settings and reload the local Django web server. python manage.py migrate and python manage.py runserver which results in :

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 19, 2020 - 16:23:37
Django version 3.0.5, using settings 'weather_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

We notice that the message for unapplied migrations has disappeared after running migrate. Now, open a web browser to http://127.0.0.1:8000/ to confirm our project is successfully installed.

Successful Installation Congrats, pal. You’ve successfully installed Django on your system.

In next tutorial, I will be guiding to create an app inside the Django Project.