English 中文(简体)
Tell django用于搜索模版子夹
原标题:Tell django to search app s template subfolders

我在座标上的模板结构如下:

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

采用模型1和通用清单词表,现在仅搜索模板/app/model1_form.html。 难道我可以告诉 d干,他也应该找回 app子。 我不想手工确定模板名称和路径(template_name=“templates/app/model1/model1_form.html”)。

在环境中。 py I have:

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

我认为:

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

提前感谢!

最佳回答

页: 1 (如果它尚未做到)。

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

然后,改变您的双倍结构,使您在名录中拥有一个“插板”:

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

或者适当命名模型,以便它们不会意外地与其他名称发生冲突:

- <project_root>
    - app
        - templates
            - app
                - model1
                - model2
问题回答

当时的其他答案是正确的,但对于现在来到这里的任何人来说,这就是在Django 1.8+1中如何做到这一点。

否则,Django现在看着模板的夹。 页: 1 App_DIRS : real, in TEMPLATES where such as:

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

如其他答复所示,您仍应使用<代码> 申请名称/项目编号/申请名称/格式>。

(更多信息:https://docs.djangoproject.com/en/1.8/intro/tutorial03/#write-views-该-实际上是-do-something 方框中题为“间隔”的模板名称

最终检查:确保你使用的是INSTALLED_APPStuple,因为这是我面临的问题。

我认为,通常需要从子里使用模板:

  1. Manually give the template path as subdir/template.html
    eg. render_to_response( sub/dir/template.html ) or template_name= ...

  2. Define all the subfolders in the TEMPLATE_DIRS
    eg.

    TEMPLATE_DIRS = (
    BASE_PATH+ /templates/ ,
    BASE_PATH+ /templates/app ,
    BASE_PATH+ /templates/app/model1 ,
    )

    But because Generic Views except templates to be found in app_name/template.html, you would have to move your templates to add /path/to/templates/app/model1 to your TEMPLATE_DIRS, and move your templates to templates/your_app/model/your_app/model_template.html, which is little bit awkward.

With Django 2.0, it s not even necessary to change anything in the TEMPLATES variable in settings.py. The only requirement is to add appname.apps.AppnameConfig (e.g. catalog.apps.CatalogConfig for an app named catalog ) to the INSTALLED_APPS list, then django will look under appname/templates when it searches for templates.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签