based on multiple choice field i want to show result in template but I have no idea how to do as you can see in this model i give lunch choice to students base on lunch choice i want to show result but it is not working for ex if he select sandwich the result after submit will you sandwich will be ready and same for others
from multiselectfield import MultiSelectField
class student(models.Model):
lunch_choice = [
('Sandwich', 'Sandwich'),
('Salad', 'Salad'),
('omlete', 'omlete'),
]
name = models.CharField(max_length=70, blank=False)
classs = models.CharField(max_length70, blank=True)
lunch = MultiSelectField(choices=lunch_choice, blank=True)
def __str__(self):
return self.name
i tried in my HTML and it didn't work
{% if student.classs %}
{% if student.lunch == 'Sandwich' %}
<p> your sandwich will be ready</p>
{% endif %}
{%endif%}
and in form.py using widget
widgets = {
'lunch':forms.CheckboxSelectMultiple(attrs={'id':'lunch'}),
}
my views.py
def preview(request):
student = student.objects.all()
return render(request, 'preview.html',{'student':student})
acc to me there is no such way by which u can show the result base on selected option because i also tried to use that and look for answers on internet but didn't find anything and creator of that repo is also not responding although I would recommend you to use models many to many field It will allow user to select more than one option like multiselectfield like this
first, create models
class Lunch(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
then add this in student models
lunch = models.ManyToManyField(Lunch, blank=True, related_name="lunch")
then add your option in lunch model
and it in your template to show result base on selected option
{% if student.classs %}
{% for Lunch in student.lunch.all %}
{% if Lunch.title == 'Sandwich' %}
<p> your sandwich will be ready</p>
{% endif %}
{% endfor %}
{%endif%}
it will work