Creating Your First Django Project for Backend Web Development

Sagar Patil
4 min readSep 7, 2021
Source: Freepik

In my past article “Django for Backend Web Development”, we have discussed features and advantages of using Django for backend web development. Continuing further, in this article we are going to create a Django project and app, also discuss some prerequisites and the file structure of the Django project.

Before building a Django project we will set up our python virtual environment. For those who are not familiar with the python virtual environment, it is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most of the Python developers use.

We are going to use virtualenvwrapper, which is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

Setup Virtual Environment:

Install python virtual environment:

pip install virtualenv

Test installation:

virtualenv --version

Install virtualenvwrapper:

pip install virtualenvwrapper

Once installation is complete, create a new folder for the project and navigate into it. Using the command line, we will create a virtual environment for our project.

Create a virtual environment:

mkvirtualenv name_of_your_env

To activate a virtual environment:

workon name_of_your_env
Activate virtual environment

Build Django Project and App:

Now that we have setup a virtual environment, we will install Django and actually start building our project and app.

Install Django:

pip install Django

To create a Django project:

django-admin startproject name_of_your_project

Our Django project is now ready. We have just created a project which manages all needs for every default project that you’ll build. Now we will create an app inside this project that contains specific functionality of our web application. We can create multiple apps as per our requirements. On the command line change directory to the Django project which we have created.

To create a Django app:

python manage.py startapp name_of_your_app

We are ready with our project and app. Let's run this project.

To run a Django project:

python manage.py runserver
Create & Run Django Project

Our project will start running at http://127.0.0.1:8000/

Structure of Django project

Django project and app file structure

Some important files:

  • manage.py: The file contains the code for running the server, makemigrations or migrations, and several other commands as well, which we perform in the code editor. This file is used as a command-line utility for our projects. We will use this file for debugging, deploying, and running our web applications.
  • settings.py: It contains the Django project configuration. The setting.py is the most important file, it contains some pre-installed apps and middleware that are there to provide basic functionality also we can add more applications and middleware externally.
  • views.py: In Django, web pages and other content are delivered by views. Each view is represented by a Python function (or method, in the case of class-based views). Django will choose a view by examining the URL that’s requested (to be precise, the part of the URL after the domain name).
  • urls.py: It contains all the endpoints that we should have for our website. This file tells Django that if a user comes with some URL, direct them to that particular page or website.
  • models.py: It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
  • __init__.py: files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
  • wsgi.py: It describes how a web server communicates with web applications, and how web applications can be chained together to process one request.
  • admins.py: It is used to display your models in the Django admin panel. You can also customize your admin panel.

Here we come to the end of this discussion. Now I hope you will be familiar with Django, setting up the project, and the basic file structure of the project. We will continue our discussion on Django and related topics in my upcoming articles. Please let me know if you have any thoughts, comments, or suggestions.

--

--

Sagar Patil

Data Scientist at Jio 🖥️📊 | Building Data Platform and Products for Jio | Google Cloud Engineer | Technology & Business Enthusiast