Add Important Files and Folders to Initial Django Project Structure

When starting a new Django project, Django automatically creates some files and folders for you. However, not all the files and folder which you will need for you application are created at the beginning.

This tutorial gives you an overview of the most important files and folders which you need to create manually. Let’s start.

After you created a new Django application as we explained in our previous tutorial, you will end up with the following file structure:

base_folder/                 
 ├──virtural_environment_name/
 |   ├── bin/
 |   ├── include/
 |   ├── lib/
 |   └── pip.selfcheck.json           
 ├── project_name/ 
 | ├── app_name/
 | | ├── migrations/
 | | ├── admin.py
 | | ├── apps.py
 | | ├── models.py
 | | ├── tests.py
 | | └── views.py
 | ├── project_name/
 | | ├── settings.py
 | | ├── urls.py
 | | └── wsgi.py 
 ├── db.sqllite3
 ├── manage.py
 └── requirements.txt

In this initial Django project structure some important files and folders are missing. We will explain them now and add them.

static/

We need a folder where we can put all our static files like CSS filesJavaScript filesimagesfont files, and other files.

Therefore, create a folder static inside the app folder. Inside the static folder you must create another folder which has the same name as you application. This is the default folder where Django will look for static files during development.

The folder structure will now look like this. You can place all the static files in the newly created folder

base_folder/                     
 ├── project_name/ 
 | ├── app_name/
 | | ├── static/
 | | | ├── app_name/
 | | | | ├── file.css
 | | | | ├── file.js
 | | | | ├── file.png
 | | | | ├── file.jpg
 | | | | └── file.txt

templates/

Like for the static files you also need a folder where you can put your HTML templates. Therefore create a similar folder structure to the static files. After you created it it should look like this:

base_folder/                     
 ├── project_name/ 
 | ├── app_name/
 | | ├── templates/
 | | | ├── app_name/
 | | | | ├── page1.html
 | | | | ├── page2.html
 | | | | ├── page3.html
 | | | | ├── page4.html
 | | | | └── page5.html

templatetags/

Django allows you to create custom templatetags besides the already available templatetags that Django offers by default.

Templatetags are useful functions which you can use in the templates. You can for example use the function lower to print strings as lowercase. In the template it will look like this

{{ my_string|lower }}

To create your own template tags you first need to create the folder templatetags as shown below. Inside this folder you need to create two files:  templatetags_extra.py where you will define your custom templatetags and __init__.py which is an empty file which tells Python that the directory is a package directory which can be imported.

The folder and file structure should now look like this.

base_folder/                     
 ├── project_name/ 
 | ├── app_name/
 | | ├── templatetags/
 | | | ├── __init__.py
 | | | └── templatetags_extra.py

Let’s now create a first filter. The filter will take a string as input, remove all blank spaces, and return the result.

Put the following code inside your templatetags_extra.py file.

 # -*- coding: utf-8 -*-
from django import template

@register.filter
def remove__blank_space(s):
    return str(s).replace(' ', '')

You can test the function by putting the following code inside one of you templates

{{ 'All the blank spaces should be removed now'|remove_blank_space }}

The output will be

Alltheblankspacesshouldberemovednow

Hopefully this tutorial helps you when starting new Django projects. Pleas leave a comment below.