Home > Backend Development > Python Tutorial > Django File Structure for Developers

Django File Structure for Developers

DDD
Release: 2024-09-19 03:59:29
Original
609 people have browsed it

Django File Structure for Developers

This django file structure guide will walk you through the essential elements of a django project.

Contents

  1. Project Root Directory
  2. Project Directory (e.g., you_project_name)
  3. Applications (Apps)
  4. Templates Directory
  5. Static Directory
  6. Media Directory
  7. Virtual Environment (venv/)

1. Project Root Directory

This directory contains the entire Django project. It contains

- manage.py: It is a command line utility that allows us to interact with project. Mainly use to start development server, create apps, run migrations etc.

- Project Folder (Your Project name folder): It contains setting and configurations of our project.

2. Project Directory (e.g., you_project_name)

This is a folder that has configurations for our Django projects. It include files like:

- init.py:

- settings.py: Contains setting for our projects such as configurations, database settings, installed apps, allowed hosts, middleware.

- urls.py: It contains URL for our projects (Routing requests for our views).

- asgi.py:

- wsgi.py:

3. Applications (Apps):

- models.py: It contains Data Structure for you project or we can say app's data/ structure of the database.

- views.py: Business logic (handling requests and responses)

- urls.py: your app specific url

- forms.py: structure and validation logic for the forms

- admin.py: Django admin panel(Dashboard) by registering the models (by creating a superuser and login to the Django's admin)

- apps.py:

- migrations/: Contains database migrations files.Each time you make any changes to your database you will see a new file with some random naes in this folder (e.g. 0001_initial, 0002_model_you_made_or_changes, ...)

4. Templates Directory:

- base.html:This contains shared code which is common in many files for example headers, footers which you want in your multiple pages.

*- other files that extend from base.html for specific views *: Lets say login.html, home.html etc.

5. Static Directory:It contains static files such as CSS, JavaScript, images. App specific directories or global one (as per your requirements).

6. Media Directory: User uploaded files for example documents, any other files may be a profile picture of a user etc.

7. Virtual Environment (venv/): Make a habit of creating a virtual environment for each of the django project to isolate project dependencies. It is important to note that it is essential for project specific packages without disturbing any global environment.

your_project_name/

├── manage.py
├── your_project_name/
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py

├── your_app_one/
│ ├── init.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ └── migrations/

├── your_app_two/
│ ├── init.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── views.py
│ └── migrations/

├── templates/
│ ├── base.html
│ └── home.html

└── static/
├── css/
└── js/

Conclusion
Understanding file structure before starting any projects in any language is very crucial and essential for efficient project development. I hope now it becomes easier for you all to navigate and manage your code bases.

Please feel free to comment your thoughts or any tips.
If you want all essential django commands at one place please comment

BONUS

Commands that you should know for manage.py

    **1. python manage.py runserver ** : To start the server

    **2. python manage.py makemigrations** : Creating new 
         migrations on the changes made in your models.

    **3. python manage.py migrate ** : Applying or unapplying 
         migrations
    **4. python manage.py createsuperuser**: Access to django 
         admin panel
Copy after login

The above is the detailed content of Django File Structure for Developers. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template