Django ModelForm中RadioSelect的Forloop选择

时间:2019-06-24 03:38:47

标签: django django-forms

我有选择列表。我想一一获得HTML的价值

forms.py

class ProfileForm(ModelForm):

class Meta:
    model = UserInfor
    fields = [ 'gender']
    widgets = {
        'gender': forms.RadioSelect(choices=GENDERS),
    }
    labels = {   
        'gender': "Gender",
    }

view.py

def profile(request):
user_info = ProfileForm()
return render(request, 'pages/test.html', {'user_info': user_info)

test.html

{% for choice in genders %}
     <label class="radio-inline" class="radio-inline"><input type="radio" value="???">{{ ??? }}</label>
{% endfor %}

1 个答案:

答案 0 :(得分:0)

{% for g_value, g_label in user_info.fields.gender.choices %}
    <label class="radio-inline" class="radio-inline"><input type="radio" value="{{ g_value }}">{{ g_label }}</label>
{% endfor %}