I'm having trouble with django. For some stupid reason Django wants to say that I can't pass choices as a part of the form. It stubbornly says that
'User' object has no attribute 'choices'
Which is bull because Im not referring to a User object. I'm not sure how to fix that. I just need the choice selected to the view method from the form. Does anyone know how to do it?
This is the form...
''' 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
The view looks like this...
''' 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,
})
Aucun commentaire:
Enregistrer un commentaire