Search code examples
djangodirectory-structure

why Django put application at the same level of my project folder?


since Django 1.4 (I think) django create a folder for my project when I start a project. Django add a folder for any application I created (with python manage.py startapp) at the same level of my project folder.

Project_name
   |---project_name_dir/
   |---application_dir/
   `---manage.py

I really like the following folder structure:

Project_name
   |---project_name_dir/
   |      |---application_dir/
   |      |       |-- __init__.py
   |      |       |-- models.py
   |      |       |-- tests.py
   |      |       `-- views.py
   |      |-- __init__.py
   |      |-- settings.py
   |      |-- urls.py 
   |      |-- wsgi.py
   |      |---templates/
   |      |      `---application_dir/
   |      `---static/
   |            |---css/
   |            |---font/
   |            |---img/
   |            `---js/
   |---deployment/
   |---documentation/
   |---config/
   `---manage.py

Because I have a folder with all my django files (project_name_dir/) and other directories for non django files.

So why Django put application at the same level of my project folder?


Solution

  • In Django, the position of the application directory is not considered. Django only uses the name of the application.

    Thus, the position of the application is basically a matter of convenience of the programmer.

    This is also the reason why two apps should not have the same name: even if they are imported in INSTALLED_APPS as

    ('app.app1', 'app1')
    

    Django only concerns with the last part after the dot, i.e. app1.

    So, in the end, you can use the directory structure you want, as long as the apps' names don't collide and you point to the app on INSTALLED_APPS. Because of this, if there isn't any special reason, you should put them on the project's root, like Django does.