Reputation: 2265
I want to use a for bucle in Django but with my own list (not using de model):
This works great:
{% for book in book_list %}
{{ book.title }}
{% endfor %}
But this doesn't work:
{% for book in ['Lord of the rings', 'Django for Beginners', 'Harry Potter'] %}
{{ book }}
{% endfor %}
Upvotes: 0
Views: 72
Reputation: 331
You need to pass the array list from Django views. You can't just declare a list in templates. Templates are only for the front-end. First, declare the list in Django view and pass it to the template then display the data using for loop.
In Django views do this
def index(request):
my_book_list = ['Lord of the rings', 'Django for Beginners', 'Harry Potter']
return render(request, 'index.html', {'my_book_list': my_book_list})
In Templates
{% for book in my_book_list %}
{{ book }}
{% endfor %}
Upvotes: 2