Django: mulitselect options not prepopulating in html

Multi tool use
Multi tool use


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 %}


{{ value }}{{ text }}

{{ text }}


{% 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





what does it show when you inspect element it ? does it display checked="checked" in input tag
– Bijoy
Jul 2 at 3:43


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.

HM,oA fi54 6mJKV58whJLjO164HF8TJkl,LOJdecx,r3uFty9ABMRoeK5EyENAbBz4IE9Fzq BYG7N,YhQLAaAu1NMxcjznwYI
sy0jrc84nAH 3alv60wZU,xB1pr gH

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications