Django: mulitselect options not prepopulating in html
Django: mulitselect options not prepopulating in html
I'm using django-multiselect
. On the html, all of the items are showing, but the ones that are already checked are not showing that they are checked. I was looking at the documentation, but it's coming prechecked. In particular this line of code isn't working: {% if value in myroles %}checked="checked"{% endif %}
Can someone see what I did wrong?
django-multiselect
{% if value in myroles %}checked="checked"{% endif %}
html
{% for value, text in form.role.field.choices %}
<div class="ui slider checkbox">
{{ value }}{{ text }}
<input id="id_role_{{ forloop.counter0 }}" name="{{ form.role.name }}" type="checkbox" value="{{ value }}" {% if value in myroles %}checked="checked"{% endif %}>
<label>{{ text }}</label>
</div>
{% endfor %}
models.py
aaa= 1
bbb= 2
ccc= 3
ddd= 4
eee= 5
ROLE_CHOICES = (
(aaa, 'AAA'),
(bbb, 'BBB'),
(ccc, 'CCC'),
(ddd, 'DDD'),
(eee, 'EEE'),
)
class myClass(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
role = MultiSelectField(choices=ROLE_CHOICES, default=True)
When I run this code on the html page:
{% for i in myroles %}
{{ i }}{{ value }}
{% endfor %}
I get an output of 11
This makes me confused why my above if statement doesn't work.
11
checked="checked"
<input id="id_roles_0" name="roles" type="checkbox" value="1">
– Micah Pearce
Jul 2 at 4:02
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what does it show when you inspect element it ? does it display
checked="checked"
in input tag– Bijoy
Jul 2 at 3:43