首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django显示单选按钮选择

Django显示单选按钮选择
EN

Stack Overflow用户
提问于 2019-03-04 04:55:30
回答 1查看 191关注 0票数 0

我想把我的用户模型的vaule_fields显示为可选择的单选按钮,你知道怎么做吗?

代码语言:javascript
复制
...

template.html

代码语言:javascript
复制
....

当前它们显示为输入域?!

EN

回答 1

Stack Overflow用户

发布于 2019-03-04 05:39:07

以下是显示为单选按钮的性别选择示例。

代码语言:javascript
复制
MODELS.PY ********************************************************
#GENDER CHOICES OPTIONS
    GENDER_COICES = (
    ('M', 'Male'),
    ('F', 'Female'),
    ('O', 'Other'),
   )

gender = models.CharField(max_length=3, choices=GENDER_COICES,
 default="N/A")
*****************************************************************

FORMS.PY ********************************************************
class UserQForm(UserCreationForm):
    """ This is a form used to create a user. """

    # Form representation of an image
    QUserPictureProfile = forms.ImageField(label="Profile Picture",
     allow_empty_file=True, required=False)

    password1 = forms.CharField(label="Password", max_length=255,
     widget=forms.PasswordInput())

    password2 = forms.CharField(label="Confirmation", max_length=255,
     widget=forms.PasswordInput())

    #GENDER CHOICES OPTIONS
    GENDER_COICES = (
      ('M', 'Male'),
      ('F', 'Female'),
      ('O', 'Other'),
    )

    gender = forms.ChoiceField(widget=forms.RadioSelect(), choices=GENDER_COICES)


    class Meta:

        model = QUser

        fields = ('QUserPictureProfile', 'gender',
        'email', 'first_name', 'last_name', 'date_of_birth', 'password1',
         'password2','phone_number',)

    def clean(self):
        """ Clean form fields. """

        cleaned_data = super(UserQForm, self).clean()

        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")

        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords do not match!")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54973657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档