102π
β
I donβt think that you can define a list directly in the template. You could pass a list to the template and do
{% if value in my_list %}
For example:
{% if name in 'foo,bar' %}
bla
{% endif %}
Alternatively, you could write a template tag which takes parameters like this:
{% ifinlist value "val1,val2,val3" %}
π€schneck
25π
You could write the if condition as
{% if value in 'Pass,Fail' %}
No need of template tag or list from backend
π€nandhini ks
- [Django]-Custom django admin templates not working
- [Django]-How to set True as default value for BooleanField on Django?
- [Django]-Login Page by using django forms
12π
Django Template:
{% value|ifinlist:"val1,val2,val3" %}
Template Tag:
from django import template
register = template.Library()
@register.filter(name='ifinlist')
def ifinlist(value, list):
return value in list
π€Sadia Arif
- [Django]-Add Text on Image using PIL
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
- [Django]-Cannot import name patterns
2π
How to pass a list to your template? I personaly use extra_context
attribute in generic views like this:
class MyObjectDetailView(DetailView):
model = MyObject
template_name = "path/to/object_detail.html"
extra_context = {'DISABLED': [model.ACCEPTED, model.REFUSED]}
Then in my template, for example, to disable btn:
<input class="btn"{% if object.status in DISABLED %} disabled="disabled"{% endif %}">
π€Emilio Conte
- [Django]-How to express a One-To-Many relationship in Django?
- [Django]-Django dump data for a single model?
- [Django]-Python Django Rest Framework UnorderedObjectListWarning
Source:stackexchange.com