我正在尝试使用循环在模板中列出我的类别,而django不会将变量识别为变量(我想是这样)。是的,我需要的所有静态文件都在他们的文件夹中。 以下是我的观点:
def home(request):
if request.user.is_authenticated():
return render_to_response('useractions/home.html', {
'ann' : Announcement.objects.all().order_by('-creation_date')[:3],
'categories' : ['Garden', 'Moving','Cleaning', 'Babysitting', 'Cooking', 'Others']
})
else:
return render(request, 'basicpages/index.html')
我的模板:
{% for kind in categories %}
<li id="cat1" style="background-image: url({% static "images/categories/{{ kind }}-background.jpg" %});">
<a href=''>
<span class="cbp-ig-icon"><img src="{% static "images/categories/{{ kind }}-icon.png" %}"></span>
<h3 class="cbp-ig-title">{{ kind }}</h3>
<span class="cbp-ig-category"> Dă click aici pentru mai multe</span>
</a>
</li>
{% endfor %}
在shell中我看到类似的东西:
[09/Apr/2016 16:16:54] "GET /static/images/categories/%7B%7B%20kind%20%7D%7D-icon.png HTTP/1.1" 404 1837
[09/Apr/2016 16:16:55] "GET /static/images/categories/%7B%7B%20kind%20%7D%7D-background.jpg HTTP/1.1" 404 1855
答案 0 :(得分:2)
尝试:
<img src="{% static 'images/categories/'%}{{ kind }}-icon.png">