[Fixed]-Django: 'Could not parse' error when creating an HTML list of names of objects from a database

1πŸ‘

βœ…

{% for kurs in   Kurs.objects.filter(prowadzacy__username=request.user.username) %}
    <li>{{ kurs.nazwa }}</li>
{% endfor %}

This won’t work in a template.

You will need to pass the filtered list to the template like so.

def userviewbasic(request):
    kurs = Kurs.objects.filter(prowadzacy__username=request.user.username)
    return render(request, 'polls/usersite.html', {"kurs": kurs})

And iterate over the parameter kurs in the template.

{% for kur in kurs %}
    <li>{{ kur.nazwa }}</li>
{% endfor %}

You can read more about the template language in Django here: https://docs.djangoproject.com/en/1.10/ref/templates/api/#rendering-a-context

πŸ‘€Kedar

Leave a comment