我想在 Django 模板标签中连接一个字符串,例如:
{% extend shop/shop_name/base.html %}
这里 shop_name
是我的变量,我想将它与路径的其余部分连接起来。
假设我有 shop_name=example.com
,并且我希望结果扩展 shop/example.com/base.html
。
用于:
{% with "shop/"|add:shop_name|add:"/base.html" as template %}
{% include template %}
{% endwith %}
不要将 add
用于字符串,您应该像这样定义一个自定义标签:
创建文件:<appname>\templatetags\<appname>_extras.py
from django import template
register = template.Library()
@register.filter
def addstr(arg1, arg2):
"""concatenate arg1 & arg2"""
return str(arg1) + str(arg2)
然后像@Steven 说的那样使用它
{% load <appname>_extras %}
{% with "shop/"|addstr:shop_name|addstr:"/base.html" as template %}
{% include template %}
{% endwith %}
避免使用 add
的原因:
根据 docs
此过滤器将首先尝试将两个值强制转换为整数......可以强制转换为整数的字符串将被求和,而不是连接......
如果两个变量碰巧都是整数,结果将出乎意料。
add
”单独只是不首先使用 str()
并且没有对我来说根本不起作用,而您的解决方案完美无缺
{% load <appname>_extras %}
我更改了文件夹层次结构
/shop/shop_name/base.html 到 /shop_name/shop/base.html
然后下面会起作用。
{% extends shop_name|add:"/shop/base.html"%}
现在它能够扩展 base.html 页面。
extends
的连接,因为`扩展必须是模板中的第一个模板标签
参考Concatenating Strings in Django Templates:
对于早期版本的 Django:{{ "Mary had a little"|stringformat:"s lamb." }}
“玛丽有只小羊羔。”
其他:{{“玛丽吃了一点”|加:“羊肉。” }}
“玛丽有只小羊羔。”
您不需要编写自定义标签。只需评估彼此相邻的变量。
"{{ shop name }}{{ other_path_var}}"
看看add
filter。
编辑:您可以链接过滤器,因此您可以执行 "shop/"|add:shop_name|add:"/base.html"
。但这不起作用,因为它取决于模板标签来评估参数中的过滤器,而扩展则没有。
我猜你不能在模板中做到这一点。
这个怎么样!我们有 first_name
和 last_name
,我们希望将空格 " "
分开。
{% with first_name|add:' '|add:last_name as name %}
<h1>{{ name }}</h1>
{% endwith %}
我们实际上在做的是:first_name + ' ' + last_name
从文档:
此标签可以通过两种方式使用:
{% extends "base.html" %} (带引号)使用文字值 "base.html" 作为要扩展的父模板的名称。
{% extends variable %} 使用变量的值。如果变量的计算结果为字符串,Django 将使用该字符串作为父模板的名称。如果变量的计算结果为 Template 对象,Django 将使用该对象作为父模板。
所以看起来你不能使用过滤器来操纵参数。在调用视图中,您必须实例化祖先模板或创建具有正确路径的字符串变量并将其与上下文一起传递。
我发现使用 {% with %}
标记非常麻烦。相反,我创建了以下模板标签,它应该适用于字符串和整数。
from django import template
register = template.Library()
@register.filter
def concat_string(value_1, value_2):
return str(value_1) + str(value_2)
然后使用以下命令在顶部的模板中加载模板标签:
{% load concat_string %}
然后,您可以通过以下方式使用它:
<a href="{{ SOME_DETAIL_URL|concat_string:object.pk }}" target="_blank">123</a>
我个人发现使用它要干净得多。
@error 的回答基本上是正确的,您应该为此使用模板标签。但是,我更喜欢更通用的模板标签,我可以用它来执行任何类似的操作:
from django import template
register = template.Library()
@register.tag(name='captureas')
def do_captureas(parser, token):
"""
Capture content for re-use throughout a template.
particularly handy for use within social meta fields
that are virtually identical.
"""
try:
tag_name, args = token.contents.split(None, 1)
except ValueError:
raise template.TemplateSyntaxError("'captureas' node requires a variable name.")
nodelist = parser.parse(('endcaptureas',))
parser.delete_first_token()
return CaptureasNode(nodelist, args)
class CaptureasNode(template.Node):
def __init__(self, nodelist, varname):
self.nodelist = nodelist
self.varname = varname
def render(self, context):
output = self.nodelist.render(context)
context[self.varname] = output
return ''
然后你可以在你的模板中像这样使用它:
{% captureas template %}shop/{{ shop_name }}/base.html{% endcaptureas %}
{% include template %}
正如评论所提到的,此模板标签对于在整个模板中可重复但需要逻辑和其他会破坏模板的信息特别有用,或者在您希望重用通过块在模板之间传递的数据的情况下:
{% captureas meta_title %}{% spaceless %}{% block meta_title %}
{% if self.title %}{{ self.title }}{% endif %}
{% endblock %}{% endspaceless %} - DEFAULT WEBSITE NAME
{% endcaptureas %}
接着:
<title>{{ meta_title }}</title>
<meta property="og:title" content="{{ meta_title }}" />
<meta itemprop="name" content="{{ meta_title }}">
<meta name="twitter:title" content="{{ meta_title }}">
captureas 标记的功劳归于此处:https://www.djangosnippets.org/snippets/545/
和多重连接:
from django import template
register = template.Library()
@register.simple_tag
def concat_all(*args):
"""concatenate all args"""
return ''.join(map(str, args))
在模板中:
{% concat_all 'x' 'y' another_var as string_result %}
concatenated string: {{ string_result }}
您不能在 django 模板中进行变量操作。您有两个选择,要么编写自己的模板标签,要么在视图中执行此操作,
extends
对此无能为力。要么将整个模板路径放在上下文变量中并使用它,要么复制现有的模板标签并适当地修改它。
在我的项目中,我是这样做的:
@register.simple_tag()
def format_string(string: str, *args: str) -> str:
"""
Adds [args] values to [string]
String format [string]: "Drew %s dad's %s dead."
Function call in template: {% format_string string "Dodd's" "dog's" %}
Result: "Drew Dodd's dad's dog's dead."
"""
return string % args
例如,在这里,您想要连接的字符串和 args 可以来自视图。
在模板中并使用您的案例:
{% format_string 'shop/%s/base.html' shop_name as template %}
{% include template %}
好的部分是 format_string 可以重复用于模板中的任何类型的字符串格式
不定期副业成功案例分享
shop_name
,因此很危险。shop_name
传递到视图的get_context_data
中的上下文之前翻译它,请确保它是使用ugettext
而不是ugettext_lazy
翻译的。