Search
Close this search box.

Django for beginners: understanding the Django project structure

Django is a very nice framework to use, once you’re familiar with the various components. This article will go through what each component is & how we use all those components to build super awesome web apps.

When we look at our Django project, we will see something like the below. The key thing to note here is that we have two separate sub directories – one which holds all the settings for Django itself and another that stores the settings for our app. Let’s look at each item in detail.

Django project folder:

In the main project folder, I have defined a folder called /static. This is where all the static content for the website is held (e.g. CSS, Javascript, Images, etc..). We will see in settings.py how Django knows what this folder is used for.

Next, we have manage.py – this is how we manage our Django project. As an example, when you make a change to your schema in your data models, they will not take effect until you migrate them, using the manage.py file (python3 manage.py makemigrations).

Django project:

The two key features of the Django project directory are settings.py and urls.py.

Settings.py defines all the settings related to your Django project. This includes the below. Without this file being correct, your Django application will never work. Additionally, without understanding the contents of the file, your Django application may remain unsecure & exposed to being hacked. We will go through security settings in detail in the next post:

  • Database configuration (usernames, passwords, database names)
  • App security settings (e.g. session & cookie settings)
  • Static directory path
  • Path to HTML templates folder
  • Etc..

urls.py maps url patterns to particular views (HTML templates & functions). This without the urls.py correctly defined, users will be unable to use any of the functionality on your Django website.

App

The app folder is where things get interesting. We’re now not looking at Django specific things, we’re looking at things related to your app and the functionality you want to build. The views.py file is really the cornerstone to your app functionality. With this, we say ‘when a particular URL is requested/button clicked etc…., this is the data which should be returned. For example, when a user visits the /my-orders page of your web app, all of their orders should be retrieved from the database & returned to the view, to be injected into the HTML page.

The models.py file defines your data models – the column names & data types that are required. For example, in a users table, you may choose to have the columns below, these would be defined in the models.py file:

  • User ID (integer)
  • Username (string)
  • etc..

The admin.py file is where we register the models defined in models.py, so that they show up in our admin view – this lets us login & administer the data visually, if we wish,

We also create a /templates folder, which is where all of our HTML files will live. So, when the user requests a URL, Django checks urls.py; directs the request to a specific view in views.py and the data returned from the view is injected into the HTML template.

Share the Post:

Related Posts