My form allows the user to select between two roles. I struggle to understand why I cannot display it in the template.
The model role default value, and the form role initial values, are both set to 'regular_user'. The models ae migated and the om values ae displayed as needed, but without the deault value.
Any input on what I'm doing wrong would be highly appreciated.
models.py:
ROLES = (('regular_user', 'Regular_user'), ('collaborator', 'Collaborator'))
class CustomUser(AbstractUser):
display_name = models.CharField(verbose_name=("Display name"), max_length=30, help_text=("Will be shown e.g. when commenting"))
...
role = models.CharField(choices = ROLES, max_length = 50, default = 'regular_user', help_text =("Click below Collaborator, if you wish to join us"))
...
class Meta:
ordering = ['last_name']
def get_absolute_url(self):
return reverse('account_profile')
def __str__(self):
return f"{self.username}: {self.first_name} {self.last_name}"
Forms.py:
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30, label=_("First name"))
...
role = forms.ChoiceField(choices=ROLES, widget=forms.RadioSelect(), label="Role", required=True, help_text=_("Click below 'Collaborator', if you wish to join us"))
...
def signup(self, request, user, **kwargs):
...
user.role = self.cleaned_data['role']
...
user.save()
If I render it with all other form fields like:
{% with "form-control input-field-"|add:field.name as field_class %}
{% render_field field class=field_class %}{% endwith %}
It renders without radio widget that cannot be checked.
When I render the role field separately, it displays properly the values, but without a default value.
{% with field=form.role %}
<small>{{ field.label_tag }}</small>
<div class="form-row">
<div class="form-group col-md-6">
{% if form.is_bound %}
{% if field.errors or form.non_field_errors %}
{% render_field field class="form-control is-invalid" %}
<div class="invalid-feedback">
{{ form.non_field_errors }}
{{ field.errors }}
</div>
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
<div class="field">
<div class="form-control">
{{ field }}
</div>
</div>
</div>
{% endif %}
</div>
</div>
{% endwith %}