Django字符串连接在模板标签最佳实践中 [英] Django string concatenation inside template tag best practice

查看:243
本文介绍了Django字符串连接在模板标签最佳实践中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接一些字符串来格式化我的模板标签中的URL,但是我没有找到一种优雅的方式。

I'm trying to concatenate some strings to format a URL inside my template tag, but I don't find an elegant way.

到目前为止,有:

{% button "Activate" "http://" site.domain url 'registration_activate' activation_key %}

有什么最佳做法可以让它更可读吗?

Is there any best practice to make it a bit more "readable"?

非常感谢

推荐答案

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

What I use when I want to concatenate strings in Django templates from variables (examples taken from my own code, tell me if you need something closer to your case):

<html>
<input id="myid_{{idBase}}_{{idFinal}}" type="checkbox"></input>
</html>

在django标签内,我使用与

and inside a django tag, I use the keyword "add" associated with the keywork with

{% with 'images/'|add:file_name as image_static %}
     <img src="{% static image_static %}" title = "{{ tooltip }}"  alt = "{{ title }}"/>
{% endwith %}

这篇关于Django字符串连接在模板标签最佳实践中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆