从自定义包含模板标签中访问 STATIC_URL [英] Access STATIC_URL from within a custom inclusion template tag

查看:15
本文介绍了从自定义包含模板标签中访问 STATIC_URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的包含模板标签,它接受单个 Update 模型对象.

模板标签:

@register.inclusion_tag('update_line.html')def update_line(更新):返回{'更新':更新}

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>

问题是 {{ STATIC_URL }} 在我的包含模板标签模板中不可用,即使我使用的是 django.core.context_processors.static 上下文处理器,因此 {{ STATIC_URL }} 可用于我所有未通过包含模板标记处理的普通"模板.

有没有一种方法可以从我的包含模板标记模板中获取 STATIC_URL ,而无需做一些讨厌的事情,例如从设置中手动获取它并将其作为上下文变量显式传递?

解决方案

好的.发布问题后才发现这一点:

我没有在包含模板中使用 {{ STATIC_URL }},而是使用了 static 模板标签中的 get_static_prefix 标签:>

update_line.html:

{% 加载静态 %}<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>

<小时>

更新

我相信现在正确的方法(django 1.5+)是:

update_line.html:

{% 加载静态文件 %}<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>

I have created a custom inclusion template tag that accepts a single Update model object.

Template tag:

@register.inclusion_tag('update_line.html')
def update_line(update):
    return {'update': update}

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>

The problem is that {{ STATIC_URL }} is not available in my inclusion template tag template, even though I am using the django.core.context_processors.static context processor so {{ STATIC_URL }} is available to all of my 'normal' templates that aren't being processed through an inclusion template tag.

Is there a way I can get the STATIC_URL from within my inclusion template tag template without doing something nasty like manually getting it from settings and explicitly passing it as a context variable?

解决方案

Okay. Just figured this out after posting the question:

Instead of using {{ STATIC_URL }} in my inclusion template, I use the get_static_prefix tag from the static template tags:

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>


Update

I believe the correct way to do this now (django 1.5+) is:

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>

这篇关于从自定义包含模板标签中访问 STATIC_URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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