django 1.5 - 如何在静态标签中使用变量 [英] django 1.5 - How to use variables inside static tag

查看:114
本文介绍了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' %}" />


推荐答案

您应该可以连接字符串与 添加模板过滤器

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屋!

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