Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从ChoiceField传递选择不接受_Python_Django_Forms_Web - Fatal编程技术网

Python 从ChoiceField传递选择不接受

Python 从ChoiceField传递选择不接受,python,django,forms,web,Python,Django,Forms,Web,我和django有麻烦了。出于某种愚蠢的原因,Django想说我不能将选择作为表单的一部分传递。它固执地说 “用户”对象没有属性“选项” 这是胡说,因为我不是指一个用户对象。我不知道如何解决这个问题。我只需要从表单中选择查看方法的选项。有人知道怎么做吗 这是表格 ''' Form used in medical personnel registration. Extends UserCreationForm ''' class MedicalPersonnelRegisterForm(UserC

我和django有麻烦了。出于某种愚蠢的原因,Django想说我不能将选择作为表单的一部分传递。它固执地说

“用户”对象没有属性“选项”

这是胡说,因为我不是指一个用户对象。我不知道如何解决这个问题。我只需要从表单中选择查看方法的选项。有人知道怎么做吗

这是表格

''' Form used in medical personnel registration. Extends UserCreationForm '''
class MedicalPersonnelRegisterForm(UserCreationForm):
#email = EmailField(required = True)
choices = [
    (Nurse, 'Nurse'),
    (Doctor, 'Doctor'),
    (HospitalAdmin, 'Admin'),
]
position = forms.ChoiceField(choices = choices)
class Meta:
    model = User
    fields = ("username", "password1", "password2")
def clean_password2(self):
    password1 = self.cleaned_data.get("password1")
    password2 = self.cleaned_data.get("password2")
    if password1 and password2 and password1 != password2:
        raise forms.ValidationError(
            self.error_messages['password_mismatch'],
            code='password_mismatch',
        )
    return password2
def save(self, commit=True):
    user=super(UserCreationForm, self).save(commit=False)
    user.set_password(self.clean_password2())
    if commit:
        user.save()
    return user
视图看起来像这样

''' This displays the Medical personnel login form. Unique because of radio box for type of user '''
def personnel_registration(request):
if request.method == "POST":
    form = MedicalPersonnelRegisterForm(request.POST)
    if form.is_valid():
        new_user = form.save()
        if new_user.choices == Nurse: # TODO : THIS MIGHT BREAK BADLY
            new_nurse = Nurse(user = new_user)
            new_nurse.save()
            temp = Event(activity= '\n'+new_nurse.user.get_full_name()+" has registered as a Nurse")
            temp.save()
        elif new_user.choices == Doctor:
            new_doctor = Doctor(user = new_user)
            new_doctor.save()
            temp = Event(activity= '\n'+new_doctor.user.get_full_name()+" has registered as a Doctor")
            temp.save()
        else:
            new_admin = HospitalAdmin(user = new_user)
            new_admin.save()
            temp = Event(activity= '\n'+new_admin.user.get_full_name()+" has registered as a Admin")
            temp.save()
        return HttpResponseRedirect('/login/') # TODO : Refer them to there home page

else:
    form = MedicalPersonnelRegisterForm()
return render(request, "main_site/personnel_registration.html", {
    "form" : form,
})

这里有很多混乱的地方,所以很难弄清楚你到底想要什么

首先,您应该注意错误发生的位置:在视图中,而不是在表单中。在它发生的地方,您有一个用户模型的实例。在任何地方都没有
choices
属性

其次,即使您正在查看表单,您仍然没有所谓的
选项
。您可以使用选项为
位置
字段设置允许的值和用户可读的内容,但
选项
本身不是字段

由于
position
是表单上的一个额外字段,与用户模型无关,因此需要从表单数据本身获取它,表单数据位于
form.cleaned\u data
。因此:

if form.cleaned_data['position'] == Nurse:
   ...

好吧,正如你在代码中的评论所说的那样,它确实严重破坏了。是什么让你们认为用户有一个属性“选择”?我不知道。唯一的问题是,我需要将其作为表单的一部分传递,以实例化用户类型。出于某种原因,它不想承认这一点,我也不知道如何让它接受位置/选择是一件事。同样的问题发生在另一个有两个CharField的类中@丹尼尔