Django模板名称冲突 [英] Django template name conflict

查看:102
本文介绍了Django模板名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Django视图-首先从调用应用程序的目录中加载模板

我有带有许多应用程序的Django项目。实际文件结构如下:

I have Django project with a number of applications. Actually file structure is following:

myproj/
  default/
    templates/
      index.html  (1)
  app1/
    templates/
      index.html  (2)
  app2/
    templates/
      index.html  (3)

我希望在解析模板时,活动应用程序的模板目录具有最高优先级。但是实际上我按照INSTALLED_APPS的顺序得到了第一个模板!
如果我更改已安装应用程序的顺序-模板也会相应更改。

I expected that active application's template directory has highest priority when template resolving. But actually I got the first template accordingly to INSTALLED_APPS order! If I changing order of installed apps - template is changed correspondingly.

问题:是否有办法首先从当前应用程序中获取模板?模板名称/显式目录规范的唯一性是实现它的唯一方法?

Question: is there way to get template from current application first? Of uniqueness of template name/explicit directory specification is the only way to achieve it?

推荐答案

通常的解决方案是将模板放入另一个以您的应用程序命名的子目录,例如:

The usual solution is to put templates in another subdirectory, named after your app, such as:

myproj/
  app1/
    templates/
      app1/
        index.html

作为 django.contrib.admin ),效果很好。我通常在模板根目录中使用通用名称,例如 index.html 来表示特定于项目的文件(例如,整个网站的索引)。

This is done by the shipped applications (such as django.contrib.admin) and works pretty well. I usually use generic names like index.html in template root for project-specific files (e.g. the index of the whole site).

这篇关于Django模板名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆