我想在Django模板标签中连接一个字符串,比如:

{% extend shop/shop_name/base.html %}

这里shop_name是我的变量,我想将它与rest of path连接起来。

假设我有shop_name=example.com,我想要结果扩展shop/example.com/base.html。


当前回答

您不需要编写自定义标记。只需要对相邻的变量求值。

"{{ shop name }}{{ other_path_var}}"

其他回答

在我的情况下,我需要concatenate发送一个参数连接到simple_tag的字符串,我不需要与,这节省了2行:

{% method firstParam "stringSecondParam="|add:valueSecondParam thirdParam as result %} 在这种情况下,问题的解决方案将是:"string="|add:object

我发现使用{% 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>

我个人觉得这样工作起来更简洁。

我已经更改了文件夹层次结构

/shop_name/shop/base.html

下面也可以。

{% extends shop_name|add:"/shop/base.html"%} 

现在它能够扩展base.html页面。

@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/

从文档中可以看出:

这个标签可以用两种方式使用:

{% extends "base.html" %}(带引号)使用字面值"base.html"作为要扩展的父模板的名称。 {% extends variable %}使用variable的值。如果变量的值是一个字符串,Django将使用该字符串作为父模板的名称。如果变量的值是一个Template对象,Django将使用该对象作为父模板。

看起来你不能用过滤器来操纵这个论证。在调用视图中,您必须实例化祖先模板,或者使用正确的路径创建一个字符串变量,并将其与上下文一起传递。