我为我的django应用程序制作了一个自定义表单,允许用户在网站上注册。
这是我的django应用程序的表单:
class TeacherRegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
school = forms.CharField(required = True)
subject = forms.CharField(required = True)
head_of_subject = forms.BooleanField(required = False)
identification_code = forms.CharField(required = True)
def __init__(self, *args, **kwargs):
super(TeacherRegistrationForm, self).__init__(*args, **kwargs)
self.fields['username'].help_text = ''
self.fields['password2'].help_text = ''
class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'school',
'identification_code',
'subject',
'head_of_subject',
'password1',
'password2'
)
def save(self, request):
form = TeacherRegistrationForm(request.POST)
user = User.objects.create(first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
email=self.cleaned_data['email'],
username=self.cleaned_data['username'],
password=self.cleaned_data['password1']
)
teacher_profile = TeacherProfile.objects.create(user=user,
school=self.cleaned_data['school'],
subject=self.cleaned_data['subject'],
head_of_subject=self.cleaned_data['head_of_subject'],
)
return user, teacher_profile
这是观点的相关部分:
if request.method == 'POST':
form = TeacherRegistrationForm(request.POST)
entered_school_name = form['school'].value()
entered_school_id = form['identification_code'].value()
actual_school_id = SchoolProfile.objects.get(school_name__exact = entered_school_name).identification_code
if form.is_valid()and (entered_school_id == actual_school_id):
user, teacher_profile = form.save(request)
return render(request, 'accounts/home.html')
else:
args = {'form': form}
return render(request, 'accounts/reg_form.html', args)
当我按提交时,会创建用户,但是没有为用户设置密码
答案 0 :(得分:2)
没有必要触及属于User的任何字段,事实上你不应该这样做,而是让UserCreationForm处理它,因为它有一些特殊的处理方式密码在那里。
在你的表单类保存方法中这样做:
def save(self):
user = super(TeacherRegistrationForm, self).save()
teacher_profile = TeacherProfile(
user=user,
school=self.cleaned_data['school'],
subject=self.cleaned_data['subject'],
head_of_subject=self.cleaned_data['head_of_subject']
)
teacher_profile.save()
return user, teacher_profile
这将导致TeacherRegistrationForm,它是UserCreationForm的子类,而UserCreationForm又是ModelForm的子类..将执行ModelForm应该做的事情,即将你自己保存到数据库(用户表)时调用它的保存方法。所以这一切都是为你完成的。然后,您只需处理为其他模型添加到表单中的额外字段。
请注意,我使用了模型save()
方法,而不是使用create()
,在这种情况下完成相同的操作..但您稍后可以修改表单代码以允许相同的表单进行编辑现有模型不仅仅是创建一个新模型。如果您希望我解释一下,请告诉我。
答案 1 :(得分:1)
尝试像这样编辑表单
def save(self):
user = User.objects.create_user(username=self.cleaned_data['username'], password=self.cleaned_data['password1'])
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
user.save()
teacher_profile = TeacherProfile.objects.create(user=user, school=self.cleaned_data['school'], subject=self.cleaned_data['subject'], head_of_subject=self.cleaned_data['head_of_subject'] )
return user, teacher_profile
使用User.objects.create_user()
而不是objects.create
。
我没有看到将request
传递给save方法的重点。您可以删除该部分。执行save
时会调用form.save()
方法。另外,在表单保存方法中再次保存表单实际上是不必要的。
答案 2 :(得分:0)
无需以自定义方式保存用户,您继承了UserCreationForm
,它将在设置密码后通过验证返回用户实例,因此您将在超级调用中获得直接用户实例。
def save(self, request):
user = super(TeacherRegistrationForm, self).save()
teacher_profile = TeacherProfile.objects.create(user=user,
school=self.cleaned_data['school'],
subject=self.cleaned_data['subject'],
head_of_subject=self.cleaned_data['head_of_subject'],
)
return user, teacher_profile