Download django to search for subfolders in the application

I have the following folder structure for templates in my django application:

templates/ app/ model1/ model1_form.html model2/ model2_form.html 

Suppose I use model1 and a generic ListView, now it is only looking for the / app / model 1_form.html templates. Anyway, can I tell django that it should also look for application / subfolders? I do not want to set the name and path to the template manually ( template_name="templates/app/model1/model1_form.html" ).

In settings.py, I have:

 import os.path BASE_PATH = os.path.dirname(os.path.dirname(__file__)) TEMPLATE_DIRS = ( BASE_PATH+'/templates/', ) 

It's my opinion:

 class HousesListView(ListView): model = House context_object_name = "house_list" 

Thanks in advance!

+6
source share
4 answers

You need to add django.template.loaders.app_directories.Loader to TEMPLATE_LOADERS (if it was not already).

 TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) 

Then change the folder structure so that the "templates" folder is in the application directory:

 - <project_root> - app - templates - model1 - model2 

Or for the correct firmware models so that they do not accidentally collide with other application names:

 - <project_root> - app - templates - app - model1 - model2 
+19
source

Other answers were correct at the time, but for those who are now faced with this, this is done in Django 1.8+

By default, Django now scans application folders for templates. This is indicated by 'APP_DIRS': True, in the TEMPLATES setting as follows:

 TEMPLATES = [ { ... 'DIRS': ['templates'], 'APP_DIRS': True, ... }, ] 

As the other answers indicate, you should still use appname/templates/appname/model1_form.html for the template namespace

(more details: https://docs.djangoproject.com/en/1.8/intro/tutorial03/#write-views-that-actually-do-something in the field called "template namespace")

Final check: make sure that the application you are using is in the INSTALLED_APPS tuple, as this was the problem I encountered.

+18
source

I think that you usually need to use templates from subfolders:

  • Manually specify the template path as subdir/template.html
    eg. render_to_response ('sub / dir / template.html') or template_name = '...'

  • Define all subfolders in TEMPLATE_DIRS
    eg.

    TEMPLATE_DIRS = (
    Base_path + '/ templates /',
    Base_path + '/ templates / applications',
    Base_path + '/ templates / application / Model1',
    )

    But since Generic Views, in addition to the templates that can be found in app_name / template.html, you will need to move your templates to add /path/to/templates/app/model1 to TEMPLATE_DIRS, and move your templates to templates/your_app/model/your_app/model_template.html , which is a bit inconvenient.

+1
source

With Django 2.0, there is no need to change anything in the TEMPLATES variable in settings.py . The only requirement is to add appname.apps.AppnameConfig (for example, catalog.apps.CatalogConfig for an application named "directory") to the INSTALLED_APPS list, then django will search under the name app / templates when searching for templates.

0
source

Source: https://habr.com/ru/post/914555/


All Articles