[Fixed]-How can I design 'base things' which could apply all apps in django?

1👍

You’d better place your templates in templates directory of the root location as well as corresponding applications , then specify in your templates section of your settings:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],

also set

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_files'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
STATIC_URL = 'http://yourdomain.com/static/'
MEDIA_ROOT = './media/'
MEDIA_URL = '/media/'


STATICFILES_DIRS = (     
     ./static_files,
)


STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

So the resulting structure is

yourproject+
           |
           +main.py
           |
           +templates+
           |         |
           +         +base.html (this is checked last)
           |
           +static_files+
           |            |
           |            +css
           |            |
           |            +js
           |     
           +media+
           |     |
           |     +images
           |
           +yourproject+
           |           |
           |           +urls.py
           |           |
           |           +wsgi.py
           |           |
           |           +settings.py         
           |
           +yourapp+admin.py
                   |
                   +models.py
                   |
                   +views.py
                   |
                   +tests.py
                   |
                   +templates+
                             |
                             +base.html (this is checked first)

This way Django will check all the templates directories within your apps, and after that the root locations’s templates will be used if no child templates found in apps to fetch base.html and any inheriting templates. Placing html templates in a separate directory will help you to keep design clean and separate from source code and configuration files.

Leave a comment