django 1.5 - 如何在静态标签中使用变量 [英] django 1.5 - How to use variables inside static tag
本文介绍了django 1.5 - 如何在静态标签中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在将项目中的所有静态文件引用迁移到 django 1.5 引入的新 {% static %} 标记,但我遇到了问题,在某些地方我使用变量来获取内容.新标签不行,有什么办法可以解决吗?
I'm currently migrating all the static files references in my project to the new {% static %} tag that django 1.5 introduced, but I'm having a problem, in some places I use variables to get the content. With the new tag I can't, is there any way to solve this?
当前代码:
<img src="{{ STATIC_URL }}/assets/flags/{{ request.LANGUAGE_CODE }}.gif" alt="{% trans 'Language' %}" title="{% trans 'Language' %}" />
它应该是什么(这不起作用):
What it should be (this doesn't work):
<img src="{% static 'assets/flags/{{ request.LANGUAGE_CODE }}.gif' %}" alt="{% trans 'Language' %}" title="{% trans 'Language' %}" />
推荐答案
您应该能够使用 add
模板过滤器:
You should be able to concatenate strings with the add
template filter:
{% with 'assets/flags/'|add:request.LANGUAGE_CODE|add:'.gif' as image_static %}
{% static image_static %}
{% endwith %}
您尝试执行的操作不适用于 static
模板标记,因为它只需要一个字符串或一个变量:
What you are trying to do doesn't work with the static
template tag because it takes either a string or a variable only:
{% static "myapp/css/base.css" %}
{% static variable_with_path %}
{% static "myapp/css/base.css" as admin_base_css %}
{% static variable_with_path as varname %}
这篇关于django 1.5 - 如何在静态标签中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文