Django字符串连接模板标签内部的最佳实践

cyb*_*oac 10 django django-templates

我正在尝试连接一些字符串来格式化我的模板标签中的URL,但我找不到一种优雅的方式.

到目前为止,我所拥有的是:

{% button "Activate" "http://" site.domain url 'registration_activate' activation_key %}
Run Code Online (Sandbox Code Playgroud)

有没有最佳做法让它更具"可读性"?

非常感谢

Zah*_*mon 14

您可以在Django模板中连接两个字符串,如下所示:

{{"First String "|add:"Second String"}}
Run Code Online (Sandbox Code Playgroud)

只需用您自己的变量替换两个字符串.


Jul*_*ard 7

当我想从变量连接Django模板中的字符串时我使用的是什么(从我自己的代码中获取的示例,告诉我你是否需要更接近你的情况):

<html>
<input id="myid_{{idBase}}_{{idFinal}}" type="checkbox"></input>
</html>
Run Code Online (Sandbox Code Playgroud)

在django标签内,我使用关键字"add"与关键字相关联

{% with 'images/'|add:file_name as image_static %}
     <img src="{% static image_static %}" title = "{{ tooltip }}"  alt = "{{ title }}"/>
{% endwith %}
Run Code Online (Sandbox Code Playgroud)