Django folder / file structure

I’m confused as a somewhat newbie. I read somewhere on Django site that the proper file / folder structure is (in part):

project - top folder
    project
          static
          staticfiles
          template
          manage.py
          requirements.txt
          etc.
          etc.
    app1
          template
                 app1
                       ...html
                       other_folders
          models.py
          urls.py
          views.py
          tables.py
          etc.
          etc.
    app2         

The issue of confusion is with what Django’s defined app folder structure app1/template/app1 naming convention and folder structure.

projects-top
      project
      app1
            template
                    app1
                         other files and folders

I’m seeing well know / respected programmers using app1/template

project-top
       project
       app1
            template
                  other files and folders

Django says that this app/template/app structure prevents issues with other apps having the same app folder name etc. etc. interference.
Can some provide clarity on this? is this also used in the main project template folder as well?
It would be nice for someone to put the definitive folder structure example here and where default Django app files would go / be at?
i.e.:

base.html
css
js
img
etc.

Sorry for the long winded question but if things like this are not well laid out plain words or easily locatable it can stifle / frustrate the new guy and he moves on to another python env.

1 Like

Side note: When posting preformatted text (including code, html, or error messages), enclose the text between lines of three backtick - ` characters. This means that you’ll have a line of ```, then the text (code, etc), then another line of ```. This forces the forum software to keep that text properly formatted. (I’ve taken the liberty of modifying your original post for you.)

First, you top example isn’t quite accurate.

You typically would not have static, staticfiles, or template in the “inner” project directory. They belong at the outer.

Not “folder” name interference, but file name interference.

Assume you have two apps, app1 and app2. In each app you have a template named template1.html.

If you have:

project
  app1
     template
       template1.html
  app2
     template
       template1.html

Then in an app2 view, you might render the template as a reference to "template1.html".

However, Django searches all template directories in order that they are defined in INSTALLED_APPS. So, what’s going to happen here is that app2 is going to find the template in app1 first, and use it instead of the template in app2. (Note: This is intentional, and a good thing - I’ll explain later on.)

On the other hand, if your directory structure is:

project
  app1
    template
      app1
        template1.html
  app2
    template
      app2
        template1.html

Then a view in app2 is going to reference this template as "app2/template1.html", avoiding the name conflict. (Similarly, app1 would refer to its template1.html as "app1/template1.html".)

Now, why is the way that Django does this a “good thing”? Because it allows you to override templates. You might install a third-party package that provides some pages with default templates. If you don’t like how those templates look, you can override those templates with your own, provided you create the same directory structure within your templates directory as that third-party product. See How to override templates | Django documentation | Django for more on this topic.

Yes, this principle applies there as well.

1 Like

Thank you Ken for confirming that I’m not loosing my mind and the best practice is

Project
     app1
          template
                app1
                    tmp.html

Tree structure.
I will do my best to remember to use the ‘’’ around my formated code on django forum.

Thank you
John