Django-如何在项目中的所有应用程序之间共享项目范围的模板标签 [英] Django -- How to have a project wide templatetags shared among all my apps in that project
问题描述
第二次询问更多详细信息...
Second time asking more details ...
我想要一个项目范围的templagetags目录,以使所有Apps都使用通用的
标签,那么每个应用程序都可以有自己的标签。
I'd like to have a project wide templagetags directory to have the common tags used by all Apps, then each app can have their own tags if need so.
让我说:
proj1/app1
proj1/app1/templatetags/app1_tags.py
proj1/app2
proj1/app2/templatetags/app2_tags.py
proj1/templatetags/proj1_tags.py
proj1/templates/app1/base.html
proj1/templates/app1/index.html
proj1/templates/app2/base.html
proj1/templates/app2/index.html
位置:
proj1/templates/app1/base.html
-----------
{% load proj1_tags %}
{% load app1_tags %}
proj1/templates/app1/index.html
-----------
{% extends "base.html" %}
proj1/templates/app2/base.html
-----------
{% load proj2_tags %}
{% load app2_tags %}
proj1/templates/app2/index.html
-----------
{% extends "base.html" %}
这项工作吗?它对我不起作用。它找不到要加载的proj1_tags。
Would this work? It didn't work for me. It can't find the proj1_tags to load.
推荐答案
我不知道这是否是正确的的方法,但是在我的Django应用程序中,我总是将常见的模板标签放在 lib
app中,如下所示:
I don't know if this is the right way to do it, but in my Django apps, I always place common template tags in a lib
"app", like so:
proj/
__init__.py
lib/
__init__.py
templatetags/
__init__.py
common_tags.py
只需确保添加 lib
应用程序添加到 settings.py
中的 INSTALLED_APPS
列表中。
Just make sure to add the lib
app to your list of INSTALLED_APPS
in settings.py
.
这篇关于Django-如何在项目中的所有应用程序之间共享项目范围的模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!