Home > database >  How Do I Render Form Attributes Manually in Django?
How Do I Render Form Attributes Manually in Django?

Time:02-10

I am trying to render the name attribute manually.

{% for language in form.languages %}
<div >
  <input  id="{{ language.id_for_label }}" name="{{ language.field.name }}" type="checkbox">
  <label  for="{{ language.id_for_label }}">{{ language.choice_label }}</label>
</div>
{% endfor %}

Everything gets rendered nicely except the name attribute of the input tag.

form.languages is a ManyToManyField shown on my form as a ModelMultipleChoiceField using the following code in my forms.py.

languages = forms.ModelMultipleChoiceField(
  queryset=Language.objects.all(),
  widget=forms.CheckboxSelectMultiple
)

EDIT: Found the culprit, apparently I need a value attribute, not a name attribute, now I just need to find a way to get the value into the template.

CodePudding user response:

I fixed the issue. What I did was use {{ language.data.value }}.

  • Related