Access STATIC_URL from within a custom inclusion template tag

后端 未结 2 441
遥遥无期
遥遥无期 2020-12-17 00:59

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

Template tag:

@register.inclu         


        
相关标签:
2条回答
  • 2020-12-17 01:13

    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>
    
    0 讨论(0)
  • 2020-12-17 01:32

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

    0 讨论(0)
提交回复
热议问题