问题
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