Django File Structure for Developers

DDD
发布: 2024-09-19 03:59:29
原创
454 人浏览过

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
登录后复制

以上是Django File Structure for Developers的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!