What I'm looking to do is package templates in a Django package which can be inserted on a developers page by simply using
我要做的是在Django包中插入包模板,只需使用它们就可以插入到开发人员页面中
{% load app_tags %}
this works find for custom methods which take a value and return a value. What I would like to do is simply have a method which returns a template packaged with the app.
这适用于接受值并返回值的自定义方法。我想要做的就是简单地用一个方法返回一个与应用程序打包的模板。
{{ custom_template }}
So the question boils down to how do I have a project which installs my app load my apps' tags and call a tag method which includes a template from the app.
所以问题就归结为我如何有一个项目安装我的应用程序加载我的应用程序的标签,并调用一个标签方法,其中包括一个来自应用程序的模板。
thank you for any responses.
谢谢您的回复。
1 个解决方案
#1
1
yup! make an inclusion tag in app_tags.py, and then call it!
是的!在app_tags中创建一个包含标记。然后叫它py !
they're great for code reuse (along with Django blocks and the {% include ... %} template tag, of course)
它们非常适合代码重用(还有Django块和{% include…)%}模板标签,当然)
reference: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags
参考:https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ # inclusion-tags
# app_tags.py
from django import template
register = template.Library()
@register.inclusion_tag("templates/myappname/greeting.html")
def greet(name, end="!!!"):
return { 'name': name, 'end': end }
and
和
{# templates/myappname/greeting.html #}
<h1> What's up {{ name }}{{ end }} </h1>
then to call this, you'd use {%
and %}
, the double bracket notation e.g. {{ custom_template }} is really only for showing the value of a single variable
然后,要调用它,您需要使用{%和%},双括号表示法,例如{{{custom_template}}实际上只用于显示单个变量的值
{% load app_tags %}
{% for person in people_to_greet %}
{% greet person %}
{% end %}
<h3> cool greetings above ^ </h3>
#1
1
yup! make an inclusion tag in app_tags.py, and then call it!
是的!在app_tags中创建一个包含标记。然后叫它py !
they're great for code reuse (along with Django blocks and the {% include ... %} template tag, of course)
它们非常适合代码重用(还有Django块和{% include…)%}模板标签,当然)
reference: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags
参考:https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ # inclusion-tags
# app_tags.py
from django import template
register = template.Library()
@register.inclusion_tag("templates/myappname/greeting.html")
def greet(name, end="!!!"):
return { 'name': name, 'end': end }
and
和
{# templates/myappname/greeting.html #}
<h1> What's up {{ name }}{{ end }} </h1>
then to call this, you'd use {%
and %}
, the double bracket notation e.g. {{ custom_template }} is really only for showing the value of a single variable
然后,要调用它,您需要使用{%和%},双括号表示法,例如{{{custom_template}}实际上只用于显示单个变量的值
{% load app_tags %}
{% for person in people_to_greet %}
{% greet person %}
{% end %}
<h3> cool greetings above ^ </h3>