
Learn Django structure best practice for the Django project structure 2022. Django is a web framework that has lots of files and folders inside it. A proper project organizing can help in keeping the project DRY (Don’t Repeat Yourself) and clean. Organizing a project is a must for every developer. This also helps the administrators and developers to find the proper path of files and folder. Best Practice to Structure Django Project Directories and Files. Django directory structure best practices.
There are lots of ways for structuring directory and files of Django project. As I a Python Developer and have been continuously working on Django projects. I founded the best practice for the Django project working directory structure.
The way I like to organize my Django Project is – Keeps all Django apps in apps folder, static files (scripts, js, CSS) in the static folder, HTML files in templates folder and images and media content in the media folder.
. ├── apps │ └── app_1 │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py ├── django_project │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-35.pyc │ │ └── settings.cpython-35.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py ├── media ├── static └── templates
Let’s create a Django Project for Django Project Structure
Create a folder in any of your directories and change the directory from the terminal.
mkdir django_project cd django_project/
Within that directory create a Virtual Environment for your Django project and activate that virtual environment.
virtualenv env source env/bin/activate
It may look like this – (env) [email protected]:~/django_project$
Now its time to install Django in your Virtual Environment, we will be using Pip3 for install Django –
pip3 install django
Now create a Django Project using the below command –
django-admin startproject django_project .
Now that we have created Django project, within the same directory we will be creating apps, media, static and templates folders.
mkdir static mkdir media mkdir templates mkdir apps
Open your django_project/settings.py with your text editor.
sudo nano django_project/settings.py
And add the following lines at the end of the file –
# Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/') # Media Files MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
Add the template path in settings.py – 'DIRS': [BASE_DIR + '/templates/',],
. You need to edit TEMPLATES
List –
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR + '/templates/',], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
Save and Exit the file.
Gather All Static File of Project to Static Folder
django.contrib.staticfiles
provides a way of gathering static files in a single directory, so you can serve them easily. We have set above STATIC_ROOT
. Now once we will run the collectstatic
command, it will copy all static files to STATIC_ROOT
Folder.
./manage.py collectstatic
It will give a message like this – 119 static files copied to 'django_project/static'.
Create Django Projects Apps
For creating Django project apps you need to change your directory, you need to go to apps
folders and inside apps folder, run the following command startapp
–
cd apps/ django-admin startapp app_1
If you will notice in your apps
folder, you will find the app_1
folder inside it.
Now, that we have created app_1
app we need to register it in settings.py
For Registering Django App, you need to open your django_project/settings.py
with your text editor and find the definition for the INSTALLED_APPS
list.
sudo nano django_project/settings.py
Add the app name which we have created to INSTALLED_APPS
list-
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'apps.app_1', # <--- here ]
‘apps.app_1
‘, – Let me break it, to help you better understand. apps
is folder name apps where have kept apps and app_1
is the app folder name.
Tip – I like to create urls.py for every app. It helps me to organize my routes. Just in your main project urls.py include the urls.py of your app.
Now we have done it. To run the project go to the main project directory and run the following command:
./manage.py runserver
Some Example that will help you –
How to use HTML file which is stored inside the templates folder.
from django.views.generic.base import TemplateView from articles.models import Article class HomePageView(TemplateView): template_name = "home.html" # templates/home.html def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['latest_articles'] = Article.objects.all()[:5] return context
How to use static files in the template which are stored in your static folder
{% load static %} <html> <head> <link rel="stylesheet" href="{% static 'app.css' %}"> <!-- static/app.css --> </head> <body> <h1>Sample</h1> <script src="{% static 'app.js' %}"></script> <!-- static/app.js --> </body> </html>