【问题标题】:How to create radio buttons in django which fields use in form and model如何在 django 中创建表单和模型中使用的字段的单选按钮
【发布时间】:2018-01-31 22:41:18
【问题描述】:

我是 Django 和 Python 的新手。我想在我的表单中添加一个单选按钮,但它不起作用。

forms.py:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.forms.widgets import RadioSelect
from choicebtn.models import myChoice


class myChoiceForm(UserCreationForm):
    name=forms.CharField(max_length=55)
    TYPE_SELECT = (('0', 'Female'),('1', 'male'),)
    gender=forms.ChoiceField(widgets.RadioSelect(choices=TYPE_SELECT))

    class Meta:
           model = myChoice
           fields = ['name','gender']   

model.py:

from django.db import models
from django.forms.widgets import RadioSelect
from django.urls import reverse


class myChoice(models.Model):
    name=models.CharField(max_length=55)
    TYPE_SELECT = (('0', 'Female'),('1', 'male'),)
    gender=models.CharField(max_length=11,choices=TYPE_SELECT)
    

这仅显示 下拉列表文本框。它不显示单选按钮。 请帮帮我。

【问题讨论】:

    标签: python django forms django-models


    【解决方案1】:

    试试这样的:

    CHOICES = [('M','Male'),('F','Female')]
    
    Gender=forms.CharField(label='Gender', widget=forms.RadioSelect(choices=CHOICES))
    

    【讨论】:

      【解决方案2】:

      在models.py中:

      class myChoice(models.Model):
          name = models.CharField(max_length=55)
          TYPE_SELECT = (
              ('0', 'Female'),
              ('1', 'male'),
          )
          gender = models.CharField(max_length=11,choices=TYPE_SELECT)
      

      在forms.py中:

      class myChoiceForm(ModelForm):
      
          class Meta:
                 model = myChoice
                 fields = ['__all__']
                 widgets = {
                     'gender': forms.RadioSelect()
                 }   
      

      【讨论】:

        【解决方案3】:

        你可以试试这个:

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

        【讨论】:

          【解决方案4】:

          正确的使用方法。

          1)models.py

          from django import forms
          
          class ExampleModel(models.Model):
              field1 = forms.ChoiceField(widget=forms.RadioSelect)
          

          2)forms.py

          class ExampleForm(forms.Form):
              field1 = forms.ChoiceField(required=True, widget=forms.RadioSelect(
              attrs={'class': 'Radio'}), choices=(('apple','Apple'),('mango','Mango'),))
          

          【讨论】:

          • django.core.exceptions.FieldError: Unknown field(s) (gender) specified for myChoice .it 显示此错误
          • 您忘记迁移模型了吗??
          • 不行。你能给我你的任何成功的代码作为参考。
          • @ArpitSingh,对不起!但是不可能将 forms.ChoiceField 放入 django 模型中。我想更好的方法是在 forms.py 中使用forms.ChoiceField(widegt=forms.RadioSelect())
          猜你喜欢
          • 2017-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-27
          • 2015-08-29
          • 2011-08-24
          • 2019-12-15
          • 2011-06-24
          相关资源
          最近更新 更多