Sebastian
Sebastian

Reputation: 573

Django does's recognize variable in img src(template)

I am trying to use a loop for listing my categories in template and django doesn't recognize the variable as a variable(I think so). And yes all the static files I need are in their folder. Here is my view:

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')

And my for in templates:

{% 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 %}

In the shell I see something like this:

[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

Upvotes: 1

Views: 1735

Answers (1)

Zorgmorduk
Zorgmorduk

Reputation: 1365

Try like:

<img src="{% static 'images/categories/'%}{{ kind }}-icon.png">

Upvotes: 5

Related Questions