我有一个注册表单,但是当我单击提交按钮时,我的数据库中没有任何显示,当我创建超级用户时,该用户已保存到数据库中,但是如果我使用该表单注册,它什么也不做。
表格.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
class RegisterForm(UserCreationForm):
class Meta:
model = User
fields = (
'first_name', 'last_name', 'email',
'password1', 'password2'
)
def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget.attrs.update({'placeholder': 'First Name', 'id': 'first-name'})
self.fields['last_name'].widget.attrs.update({'placeholder': 'Last Name', 'id': 'last-name'})
self.fields['email'].widget.attrs.update({'placeholder': 'Email Address', 'id': 'email-address'})
self.fields['password1'].widget.attrs.update({'placeholder': 'New Password', 'id': 'password1'})
self.fields['password2'].widget.attrs.update({'placeholder': 'Re-enter New Password', 'id': 'password2'})
视图.py
# login and register function
def loginAndRegister(request):
# register form
if request.method == 'POST':
registerForm = RegisterForm(request.POST)
if registerForm.is_valid():
registerForm.save()
messages.success(request, 'Your account has created successfully! you can log in now')
return redirect('login')
else:
registerForm = RegisterForm()
context = {'registerForm': registerForm}
return render(request, 'account/loginAndRegisterPage.html', context)
html视图
<form method='POST'>
{% csrf_token %}
<li>{{ registerForm.first_name }}</li>
<li>{{ registerForm.last_name }}</li>
<li>{{ registerForm.email }}</li>
<li>{{ registerForm.password1 }}</li>
<li>{{ registerForm.password2 }}</li>
<li><input type="submit" value="Create Account"></li>
</form>
它不起作用,因为我在名字和姓氏中使用了重复的名称,而且因为我使用了自定义表单,它没有向我显示任何错误,这就是为什么我不知道表单有什么问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句