I am new to python and django. I want to know how can I dispaly python list in django template. The list is a list of days in weeks e.g day_list = ['sunday','monday','tuesday']
Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.
To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.
Pass it to the template as a context and then simply iterate over it.
{% for day in day_list %}
{{ day }}
{% endfor %}
This is the documentation for the for
tag. I recommend you go through the Django tutorial, and in part 3 they go over passing stuff to your templates, including iterating over sequences of stuff (like lists).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With