DEV Community

Cover image for Let's Learn Django #2 File Structure
Rachit Khurana
Rachit Khurana

Posted on

Let's Learn Django #2 File Structure

After initialising a Django project, we can see several files. Don't worry, this blog will explain all of them in detail.

The general file structure of Django looks like this:

├── [appname]
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   └── views.py
├── [projectname]
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
Enter fullscreen mode Exit fullscreen mode

Here [appname] represents the name of the app you have created & [projectname] represents your project name.

Now, let's go over each file one by one.

manage.py

It mainly contains code to manage your Django application, like running the project, applying migrations, etc. Most of the time we would never need to edit this file. So we can even ignore it for now.

settings.py

It contains all the project configs required. It is one of the most important files. We will add more apps here, set up email servers, templating, middleware, databases, and a lot more. Don't worry, we don't need to know everything about it right away.

urls.py

In this file, we will define all the URL routes that we will be using in our projects. We can map the URL routes to a specific view, redirect to another URL etc. We can also define dynamic URLs, parameterised URLs, and much more. This allows us to configure the flow of our project and define how different URLs should be handled.

asgi.py & wsgi.py

ASGI stands for Asynchronous Server Gateway Interface & WSGI stands for Web Server Gateway Interface. Django automatically generates these files for us so we don't need to modify them initially. These files handle the communication between Django and the web server. While it's a bit of an advanced topic, you can skip it for now. If you're interested, you can read more about ASGI and WSGI online.

views.py

This is the main file where you write the main logic of the web pages. Django supports 2 types of views, function-based views & class-based views. They are used to define what needs to be done & what needs to be returned when a specific URL is called.

We can return a JSON response, A HTTP response, a rendered template or anything we want.

models.py

This file is mainly used to define all the models that we want to use. You can think of it as defining the database structure so that we can easily read & write data into the database. After defining the models here, we can use the very powerful Django ORM(Object-Relational Mapping) to interact with the database. I will cover the Django ORM in later blogs of this series.

app.py

This file is mainly used to customize the configuration of your app or define additional metadata for it. For now, we can ignore this file.

admin.py

This file is used to register our app & models in the admin panel. So that we can easily edit data in the admin panel. We can also customise the looks & behaviour of the app so that it looks & behaves as we want in the admin panel.

Migrations folder

This folder contains all the migrations files that are generated by manage.py when we edit the models in our models.py file. These migration files then make the specified changes in the main database.

With this, we have covered the basics of most of the important Django files that we would need while building a web app.

In the next blog, we will cover how to make url routes & views for them in the urls.py & views.py files respectively.

I hope this blog series is helping you learn Django.

Follow my journey on Twitter: @notnotrachit

Top comments (0)