I can't be sure which kind of structure is best, but I can share some tips for an easy to manage and clear to understand Django project structure. In this post, we'll try to go beyond the simple Django project structure. It'll be useful for beginners.
For all examples, assume that the project is named "elmadev" and is a blog project.
1. A separate folder for apps
Usually we see a structure with separate folders for each application in a Django project. One folder per application in the home dir of the project? I think this makes the structure unnecessarily complex.
We can keep all the apps in a single folder named apps
:
βββ apps <---- attention here
βΒ Β βββ __init__.py
βΒ Β βββ authors
βΒ Β βββ posts
βββ elmadev
βΒ Β βββ __init__.py
βΒ Β βββ asgi.py
βΒ Β βββ settings.py
βΒ Β βββ urls.py
βΒ Β βββ wsgi.py
βββ manage.py
In the settings.py
file, we can add that apps
folder to the path:
sys.path.append(str(BASE_DIR / 'apps'))
With this way, we can still import any modules like we do before:
from authors.views import AuthorDetailView
from posts.views import PostDetailView
2. Custom name for the main folder
I think it's more clear to use a generic name, rather than naming the main folder the same as the project name.
$ django-admin startproject elmadev
The above command creates a structure like below:
elmadev
βββ elmadev # <--- attention here
βΒ Β βββ __init__.py
βΒ Β βββ asgi.py
βΒ Β βββ settings.py
βΒ Β βββ urls.py
βΒ Β βββ wsgi.py
βββ manage.py
But, the structure we want is as follows:
elmadev
βββ main # <--- attention here
βΒ Β βββ __init__.py
βΒ Β βββ asgi.py
βΒ Β βββ settings.py
βΒ Β βββ urls.py
βΒ Β βββ wsgi.py
βββ manage.py
if you are going to create a new project, you can easily achieve this using the following command:
$ django-admin startproject main
But if you want to have that structure in an already created project, try this:
- Rename the folder
elmadev
tomain
. - Update the paths containing
elmadev
according to the new folder namemain
:- manage.py:9, wsgi.py:14, asgi.py:14:
elmadev.settings
>main.settings
- main/settings.py:52:
ROOT_URLCONF = 'elmadev.urls'
>ROOT_URLCONF = 'main.urls'
- main/settings.py:70:
WSGI_APPLICATION = 'elmadev.wsgi.application'
>WSGI_APPLICATION = 'main.wsgi.application'
- manage.py:9, wsgi.py:14, asgi.py:14:
settings.py
files for different development stages
3. Multiple You can use different settings.py
files for development, testing and deployment stages. There are various approaches for this. Let's go through an easy-to-understand example.
Since we changed the name of our main project folder in the previous example, it is now main
.
-
Name the
settings.py
file in themain
folder asbase.py
. -
Create a folder named
settings
in themain
folder and move thebase.py
file into it. Don't forget to also create an empty__init__.py
file in the settings folder. -
Create a new file named
development.py
and add the following codes:from base import * DEBUG = True # ...
-
Here you can override all the settings you want to change for the development stage, like in the
DEBUG = True
line. -
Change the
main.settings
path in theasgi.py
and thewsgi.py
files tomain.settings.base
. -
For example, if you define
DJANGO_SETTINGS_MODULE = 'main.settings.development
variable in the virtual environment you use for development, Django will use thedevelopment.py
module while in this env.
requirements.txt
files:
4. Separate You can have multiple requirements.txt
files like requirements.txt
and requirements-dev.txt
and in the requirements-dev.txt
file, you can import the content of the requirements.txt
file like below:
-r requirements.txt
With this way, when you install dependencies with the command pip install -r requirements-dev.txt
, it will also install the dependencies listed in the requirements.txt
file.
Many more suggestions could be added, but the article is getting long. Maybe I'll add more tips in the future.