繁体   English   中英

Django:__init__() 得到了一个意外的关键字参数“选择”

[英]Django: __init__() got an unexpected keyword argument 'choices'

我正在尝试创建一个表单,并将用户输入的值传递到数据库中。

models.py

from django.db import models

class Evangelized(models.Model):
    full_name = models.CharField(max_length = 128)
    email = models.EmailField()
    mobile_no = models.CharField(unique = True, max_length = 128)
    twitter_url = models.CharField(unique = True, max_length = 128)
    GENDER_CHOICES = (('M', 'Male'), ('F', 'Female'), ('U', 'Unisex/Parody'))
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    AREA_CHOICES = (('Govt', 'Govt'), ('Entertainment', 'Entertainment'), ('Automobile', 'Automobile'),
                        ('Careers', 'Careers'), ('Books', 'Books'), ('Family', 'Family'), ('Food', 'Food'),
                            ('Gaming', 'Gaming'), ('Beauty', 'Beauty'), ('Sports', 'Sports'), ('Events', 'Events'),
                                ('Business', 'Business'), ('Travel', 'Travel'), ('Health', 'Health'), ('Technology','Technology'))
    area_of_interest = models.CharField(choices = AREA_CHOICES, max_length = 128)
    other_area_of_interest = models.CharField(blank = True, max_length = 128)
    city = models.CharField(max_length = 128)
    BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))
    other_social_accounts = models.BooleanField(choices = BOOL_CHOICES, default = None)
    questions = models.CharField(blank = True, max_length = 1280)
    about_yourself = models.CharField(blank = False, max_length = 1280)
    referrer = models.CharField(max_length = 128)

表格.py

   from django import forms
from rango.models import Evangelized

class EvangelizedForm(forms.ModelForm):
    full_name = forms.CharField(help_text="Full Name")
    email = forms.CharField(help_text="Email ID")
    mobile_no = forms.CharField(help_text="Mobile number")
    twitter_url = forms.CharField(help_text="Twitter URL")
    gender = forms.ChoiceField(widget=forms.RadioSelect(), 
                 choices=Evangelized.GENDER_CHOICES, help_text="Gender")
    area_of_interest = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(),
                                                    choices=Evangelized.AREA_CHOICES, help_text="Areas of interest(Upto 3)")
    """def clean_area_of_interest(self):
        if len(self.cleaned_data['area_of_interest']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['area_of_interest']"""
    other_area_of_interest = forms.CharField(help_text="Other area of interest")
    city = forms.CharField(help_text="City")
    other_social_accounts = forms.CharField(widget=forms.RadioSelect(),choices=Evangelized.BOOL_CHOICES, help_text="Do you have other social accounts?", max_length=128)
    questions = forms.CharField(help_text="Do you have any questions?")
    about_yourself = forms.CharField(help_text="Tell us about yourself")
    referrer = forms.CharField(help_text="I heard about this platform via:")

    class Meta:
        model = Evangelized
        fields = ('full_name', 'email', 'mobile_no', 'twitter_url', 'gender', 'area_of_interest', 'other_area_of_interest', 'city', 'other_social_accounts',
                    'questions', 'about_yourself', 'referrer')

视图.py

def fillform(request):
    if request.method == 'POST':
        form = EvangelizedForm(request.POST)
        if form.is_valid():
            form.save(commit = True)
            return index(request)
        else:
            form.errors
    else:
        form = EvangelizedForm()

    return render(request, 'rango/fillform.html', {'form': form})

但是,我遇到以下错误:

Exception Type: TypeError at /rango/
Exception Value: __init__() got an unexpected keyword argument 'unique'

我的代码似乎有什么问题? 这个错误意味着什么?

编辑:

我已经修改了我的forms.py文件的源代码,并且在原始标题中也有一些机会。 似乎我的表单字段参数中的choices键产生了错误。 但是,代码在语法上是正确的,对吗?

例如,像您的mobile_no这样的表单字段没有unique参数。 只有模型字段有。 您必须从表单模型中删除所有unique参数。

我已经弄清楚是什么导致了这个错误。 choices参数应该在forms.py的小部件构造函数中forms.py如下:

from django import forms
from rango.models import Evangelized

class EvangelizedForm(forms.ModelForm):
    full_name = forms.CharField(help_text="Full Name")
    email = forms.CharField(help_text="Email ID")
    mobile_no = forms.CharField(help_text="Mobile number")
    twitter_url = forms.CharField(help_text="Twitter URL")
    gender = forms.ChoiceField(widget=forms.RadioSelect(
                 choices=Evangelized.GENDER_CHOICES), help_text="Gender")
    area_of_interest = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(
                                                    choices=Evangelized.AREA_CHOICES), help_text="Areas of interest(Upto 3)")
    """def clean_area_of_interest(self):
        if len(self.cleaned_data['area_of_interest']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['area_of_interest']"""
    other_area_of_interest = forms.CharField(help_text="Other area of interest")
    city = forms.CharField(help_text="City")
    other_social_accounts = forms.CharField(widget=forms.RadioSelect(choices=Evangelized.BOOL_CHOICES), help_text="Do you have other social accounts?", max_length=128)
    questions = forms.CharField(help_text="Do you have any questions?")
    about_yourself = forms.CharField(help_text="Tell us about yourself")
    referrer = forms.CharField(help_text="I heard about this platform via:")

    class Meta:
        model = Evangelized
        fields = ('full_name', 'email', 'mobile_no', 'twitter_url', 'gender', 'area_of_interest', 'other_area_of_interest', 'city', 'other_social_accounts',
                    'questions', 'about_yourself', 'referrer')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM