Django Project Structure and File Structure

Django makes use of a directory structure to arrange different parts of the web application. It creates a project and an app folder for this. 

Creating a proper project and organizing it helps in keeping the project DRY (Don’t Repeat Yourself) and clean.

When we create a Django project, the Django itself creates a root directory of the project with the project name you have given on it. It contains the necessary files that would provide basic functionalities to your web applications. 

Different files in Django Root Directory

You can search for the directory by searching the name of the project you have created using the command django -admin startproject (project_name).

We will be using Visual studio for further work. I will recommend this to you as well.

1. manage.py

If you are not new to Django, you must have seen the word “command-line utility” many times. But what does this mean?

Well, by using the command-line utility, you can migrate i.e., either import or export metadata objects across different domains or setup. You can create one or more objects within an object or its multiple objects.

Now straight away coming to the topic. What is manage.py?

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.

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.

It performs the same things as django-admin but also provides some project-specific functionality. 

There are several commands under manage.py. Few important ones are as follows:

a. Runserver

This command is used to start the test server for our web application, provided by the Django framework.

b. makemigrations

We use this command to apply new migrations across projects and apps that have been carried out due to the changes in the database.

c. migrate

This is the prior step of the makemigration command. We use this for making the changes to the modules in the database. 

Below in the image is the list of tasks you can perform with manage.py.

Django Project Structure

Project files

Project is the name you have given to your project while typing django -admin startproject (project_name). In my case it is TechVidvan. It contains configuration files of the project.

Django Project Layout

1. __init.py_

This is an empty file as you can see below in the image. The function of this file is to tell the Python interpreter that this directory is a package and involvement of this __init.py_ file in it makes it a python project.

Django Project Structure

2. settings.py

It contains the Django project configuration.

The setting.py is the most important file, and it is used for adding all the applications and middleware applications. This is the main setting file of the Django project. 

This contains several variable names, and if you change the value, your application will work accordingly.

It contains sqlite3 as the default database. We can change this database to Mysql, PostgreSQL, or MongoDB according to the web application we create.

It contains some pre-installed apps and middleware that are there to provide basic functionality.

Django Project Layout

3. urls.py

URL is a universal resource locator, it contains all the endpoints that we should have for our website. It is used to provide you the address of the resources (images, webpages, websites, etc) that are present out there on the internet.

In simpler words, this file tells Django that if a user comes with this URL, direct them to that particular website or image whatsoever it is.

Django Layout

4. wsgi.py

When you will complete your journey from development to production, the next task is hosting your application. Here you will not be using the Django web server, but the WSGI server will take care of it

WSGI stands for Web Server Gateway Interface, it describes the way how servers interact with the applications.

It is a very easy task, you just have to import middleware according to the server you want to use. For every server, there is Django middleware available that solves all the integration and connectivity issues.

Django Project Structure

5. asgi.py

ASGI works similar to WSGI but comes with some additional functionality.  ASGI stands for Asynchronous Server Gateway Interface. It is now replacing its predecessor WSGI.

Django Project Layout

 

Django Application Files

Django uses the concept of Projects and apps for managing the codes and presents them in a readable format. A Django project contains one or more apps within it, which performs the work simultaneously to provide a smooth flow of the web application. 

For example, a real-world Django e-commerce site will have one app for user authentication, another app for payments, and a third app for item listing details: each will focus on a single functionality.

In the example below, we have created a Project named “TechVidvan” and an App called “HelloWorld”

Type the following command in the terminal:

Python manage.py startapp HelloWorld

Django Project LayoutOnce the app is created, the following sub-files will be present under that app.

1.  _init_.py

This file provides the same functionality as that in the _init_.py file in the Django project structure. It is an empty file and does not need any modifications. It just represents that the app directory is a package.

2.  admin.py

Admin.py file is used for registering the Django models into the Django administration.

It is used to display the Django model in the Django admin panel. It performs three major tasks:

a. Registering models

b. Creating a Superuser

c. Logging in and using the web application

We will learn more about the admin panel in the next article about Admin Interface.

3. apps.py

Apps.py is a file that is used to help the user include the application configuration for their app.

Users can configure the attributes of their application using the apps.py file.

However, configuring the attributes is a rare task a user ever performs, because most of the time the default configuration is sufficient enough to work with.

4. models.py

Models.py represents the models of web applications in the form of classes. It is considered the most important aspect of the App file structure.

Models define the structure of the database. It tells about the actual design, relationships between the data sets, and their attribute constraints. 

5. views.py

Views are also an important part when we talk about the Django app structure. Views provide an interface through which a user interacts with a Django web application. It contains all the views in the form of classes.

We use the concept of Serializers in Django Rest_Framework for making different types of views. Some of these are CustomFilter Views, Class-Based List Views, and Detail Views.

6. urls.py

Urls.py works the same as that of the urls.py in the project file structure. The primary aim being, linking the user’s URL request to the corresponding pages it is pointing to.

You won’t find this under the app files. We create this by clicking on the New file option written on the top, after the Project name.

7. tests.py

Tests.py allows the user to write test code for their web applications. It is used to test the working of the app.

Its working is quite complex. We will discuss it in more detail in the upcoming articles.

Summary

This brings us to the end. In this tutorial, we have discussed the Django project layout.

All the files we have discussed above are within every Django application you create. The main aim of these files is to provide you with backend support. 

However, the settings.py and urls.py are the two main files we will be working with. Making changes to these files will bring unique functionalities to the web application you create.