Access STATIC_URL from a custom inclusion template tag

I created my own 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 }} not available in the inclusion template tag template, although I use the django.core.context_processors.static context django.core.context_processors.static , so {{ STATIC_URL }} is available to all my "normal" templates that are not processed with using the inclusion template tag.

Is there a way to get STATIC_URL from my inclusion template tag template without doing something unpleasant, how to manually get it from the settings and explicitly pass it as a context variable?

+7
source share
2 answers

Good. Just thought about it 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 it now (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> 
+14
source

Inside the template tag code, you can do what you like: you can easily import STATIC_URL from settings yourself.

+2
source

All Articles