3

I'm a reader and a big fan of the popular book Two Scoops of Django. The book is full of great ideas, but there is one which is not that clear to me.

The authors advice to create a static and a templates folder in the Django project root.
That template folder is meant for "site-wide templates" (stated at pag.24). Plus at pag.162 they say that "templates should usually go into the root folder of the Django project... The only exception is when you bundle up an app into a third-party package".

They don't mention it explicitly in ch.12 but I guess it is good to create a folder for each app (with the same name as the app) in the templates root.
Let's suppose that in my icecreamratings Django project I have 2 apps:

  • flavors with 2 templates: new_flavor.html and details.html
  • profiles with 1 template: details.html All templates inherit from base.html.

I guess they would suggest the following structure:

icecreamratings_project/
  |-- ...
  |-- icecreamratings/  # Django project root
        |-- ...
        |-- static/
        |-- templates/
              |-- 404.html
              |-- 500.html
              |-- base.html
              |-- flavors/  # same name as app flavor
              |     |-- new_flavor.html
              |     |-- details.html
              |-- profiles/  # same name as app profiles
                    |-- details.html

Did I get it correctly?

On the other hand, the Django official docs suggests to create a static folder in each app and, inside it, a subfolder with the same name as the app, like: my_app/static/my_app/myimage.jpg.

So we have 2 different strategies: a unique template folder and many static folders (one in each app). Having two different strategies is clearly a bad choice.

So, what do you think of storing in each app a template and static folder? Something like this:

icecreamratings_project/
  |-- ...
  |-- icecreamratings/  # Django project root
        |-- ...
        |-- static/  # site-wide static files
        |     |-- bootstrap/  # bootstrap source files
        |     |-- jquery/  # jquery source files
        |     |-- css/  # css files used by base.html (and others in templates root)
        |     |-- js/  # javascript files used base.html (and others in templates root)
        |     |-- img/  # img files files used base.html (and others in templates root)
        |-- templates/  # site-wide templates
        |     |-- 404.html
        |     |-- 500.html
        |     |-- base.html
        |-- flavors/  # an app
              |-- static/
              |     |-- flavors/  # static files specific for this app
              |           |-- css/  # css files for flavor app templates
              |           |-- js/  # javascript files for flavor app templates
              |           |-- img/  # img files for flavor app templates
              |-- templates/  # template files specific for this app
                    |-- new_flavor.html
                    |-- details.html

2 Answers 2

0

The django official doc does not suggest your to create a static folder in each app. Instead it prefer to store the static dir under the project dir.

Static file namespacing

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

The official doc also answered your question about templates structure.

class app_directories.Loader

Loads templates from Django apps on the filesystem. For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates in there.

This means you can store templates with your individual apps. This also makes it easy to distribute Django apps with default templates.

The Two Scoops of Django is a great book. But you should read the official doc first.

0

About templates, as per Django official docs (https://docs.djangoproject.com/en/3.2/ref/templates/api/#loader-types):

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'], #add this line
        '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',
            ],
        },
    },
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.