Django string concatenation inside template tag best practice

穿精又带淫゛_ 提交于 2019-12-04 18:14:31

问题


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

So far, what I have is:

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

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

Thanks a lot


回答1:


You can concatenate two strings in Django template as follows:

{{"First String "|add:"Second String"}}

Just replace the two strings with your own variable.




回答2:


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>

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 %}


来源:https://stackoverflow.com/questions/27708849/django-string-concatenation-inside-template-tag-best-practice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!