dge*_*gel 7 django django-templates
我创建了一个自定义包含模板标记,它接受单个Update模型对象.
模板标签:
@register.inclusion_tag('update_line.html')
def update_line(update):
return {'update': update}
Run Code Online (Sandbox Code Playgroud)
update_line.html:
<tr><td class="update">{{ update }}</td><td class="ack">
<img id="update-{{ update.pk }}" class="ack-img" src="{{ STATIC_URL }}img/acknowledge.png" alt="Acknowledge" /></td></tr>
Run Code Online (Sandbox Code Playgroud)
问题是{{ STATIC_URL }}我的包含模板标签模板中没有这个问题,即使我正在使用django.core.context_processors.static上下文处理器,因此{{ STATIC_URL }}我可以使用所有未通过包含模板标签处理的"普通"模板.
有没有一种方法可以STATIC_URL从我的包含模板标签模板中获取,而不会做一些令人讨厌的事情,比如手动从设置中获取并明确地将其作为上下文变量传递?
dge*_*gel 14
好的.在发布问题后刚想出来:
{{ STATIC_URL }}我使用模板标签中的get_static_prefix标签,而不是在我的包含模板中使用static:
update_line.html:
{% load static %}
<tr><td class="update">{{ update }}</td><td class="ack">
<img id="update-{{ update.pk }}" class="ack-img" src="{% get_static_prefix %}img/acknowledge.png" alt="Acknowledge" /></td></tr>
Run Code Online (Sandbox Code Playgroud)
我相信现在这样做的正确方法(django 1.5+)是:
update_line.html:
{% load staticfiles %}
<tr><td class="update">{{ update }}</td><td class="ack">
<img id="update-{{ update.pk }}" class="ack-img" src="{% static 'my_app/img/acknowledge.png' %}" alt="Acknowledge" /></td></tr>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3590 次 |
| 最近记录: |