Python 自定义用户注册表单:Django HTML mashup?
我已经建立了一个用户注册表单,我希望是一个正常工作的后端注册机制(据我所知,这不起作用,因为我需要在表单中定义密码验证方法): forms.pyPython 自定义用户注册表单:Django HTML mashup?,python,html,django,django-forms,django-views,Python,Html,Django,Django Forms,Django Views,我已经建立了一个用户注册表单,我希望是一个正常工作的后端注册机制(据我所知,这不起作用,因为我需要在表单中定义密码验证方法): forms.py class UserRegistrationForm(forms.ModelForm): password_setp = forms.CharField(label = 'Password', widget = forms.PasswordInput) password_conf = forms.CharField(label = 'R
class UserRegistrationForm(forms.ModelForm):
password_setp = forms.CharField(label = 'Password', widget = forms.PasswordInput)
password_conf = forms.CharField(label = 'Repeat Password', widget = forms.PasswordInput)
class Meta:
model = User
fields = ( 'username', 'first_name', 'last_name', 'email', 'password_setp', 'password_conf',)
def passwordconf_validation(self):
cleaned_pw = self.cleaned_data
if cleaned_pw['password_setp'] != cleaned_pw[password_conf]:
raise forms.ValidationError("I\'m sorry, your chosen passwords do not match. Please try again.")
return cleaned_data['password_conf']
def __init__(self, *args, **kwargs):
super(UserRegistrationForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
self.fields['first_name'].required = True
self.fields['last_name'].required = True
views.py:
from django.views.generic import CreateView
class UserRegistrationView(CreateView):
template_name = 'oauth/user/registration_form.html'
model = User
fields = { 'username', 'first_name', 'last_name', 'email', }
def get_queryset(self):
"""
Get the list of items for this view. This must be an interable, and may
be a queryset (in which qs-specific behavior will be enabled).
"""
if self.queryset is not None:
queryset = self.queryset
if hasattr(queryset, '_clone'):
queryset = queryset._clone()
elif self.model is not None:
queryset = self.model._default_manager.all()
else:
raise ImproperlyConfigured(u"'%s' must define 'queryset' or 'model'"
% self.__class__.__name__)
return queryset
def user_registration(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
#Create a new user object instance, but avoid saving it until the password is validated:
new_user = user_form.save(commit=False)
#Set the chosen password if password validated:
new_user.set_password(user_form.cleaned_data['password'])
#Save the User object
new_user.save()
return render(request, 'blog/post/list.html', { 'new_user': new_user })
else:
user_form = UserRegistrationForm(request.POST)
return render(request, 'oauth/user/registration_form.html', { 'user_form': user_form })
我有一个问题-密码设置和确认字段没有呈现(password\u setp
,password\u conf
)
一旦呈现正确,我想做的就是自定义以下html以接受UserRegistrationForm类中定义的所有必需字段:
<form class="contact-form" name="contact-form" method="post" action=".">
<div class="form-group">
{% csrf_token %}
<input type="text" class="form-control" required="required" placeholder="Forename">
<input type="text" class="form-control" required="required" placeholder="Surname">
<input type="text" class="form-control" required="required" placeholder="Username">
<input type="email" class="form-control" required="required" placeholder="Email">
<input type="password" class="form-control" required="required" placeholder="Password">
<input type="password" class="form-control" required="required" placeholder="Confirm Password">
<button type="submit" class="btn btn-primary">Sign In</button>
</div>
</form>
{%csrf_令牌%}
登录
您如何在CreateView中调用'user\u registration(request)
方法
'def passwordconf\u验证(自)
'
这应该是
def clean_passwordconf(self, **kwargs):
您还可以为已进行密码验证的创建用户继承django内置UserCreationForm
Hi Neeraj,我正在使用django.views.generic import CreateView中的
继承这个类,但是CreateView没有任何在“user\u registration”中命名的方法,所以它将如何调用?。在这里检查CreateView的所有方法啊哈…好吧,这是数字…那么我应该从哪个视图继承?只需要在CreateView中添加form_class=UserRegistrationForm,它将在模板中为您提供“form”对象,这样您就可以使用模板中的form对象来渲染字段或formOk-我应该传入哪个视图?