How do you place a variable inside a template tag's argument?

谁都会走 提交于 2019-12-08 05:55:17

问题


With Django media I could do the following:

{% for language in LANGUAGES %}
  <script type="text/javascript" src="{{ MEDIA_URL }}app/js/jquery.ui.datepicker.{{ language.0 }}.js"></script>
{% endfor %}

Now, I'd like to switch to static files. However this (obviously) does not work any more:

{% for language in LANGUAGES %}
  <script type="text/javascript" src="{% static "app/js/jquery.ui.datepicker.{{ language.0 }}.js" %}"></script>
{% endfor %}

Is there some clean way to achieve what I'm trying to do?

[ I want to switch for consistency, because the admin and many third-party apps I'm using have switched. Also, I'm worried that the Django media will be deprecated sometime in the future. ]


回答1:


You have two options. First one, as suggested, use {{ STATIC_URL }}:

{% for language in LANGUAGES %} 
<script type="text/javascript"
  src="{{STATIC_URL}}app/js/jquery.ui.datepicker.{{ language.0 }}.js"
></script> 
{% endfor %}

Alternatively, as {% static %} only prefixes its argument but does not require the path to be actually valid:

{% for language in LANGUAGES %} 
<script type="text/javascript"
  src="{% static "app/js/jquery.ui.datepicker" %}.{{ language.0 }}.js"
></script>                                 
{% endfor %}


来源:https://stackoverflow.com/questions/17220632/how-do-you-place-a-variable-inside-a-template-tags-argument

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