简体   繁体   English

Django UserCreationForm未在模板中呈现错误

[英]Django UserCreationForm not rendering errors in template

I am using django's UserCreationForm to sign up users. 我正在使用django的UserCreationForm来注册用户。 It works perfectly except for the errors. 除错误外,它运行完美。 I cannot get them to render. 我无法渲染它们。 I don't think there is anything wrong with the template as I have tried this with the most basic of templates and using form.as_p and form.as_table and still the same the registration works but if you put 2 different passwords in it just refreshes the screen with an empty form and no errors. 我认为模板没有任何问题,因为我已经尝试过使用最基本的模板并使用form.as_p和form.as_table,并且仍然可以进行注册,但是如果您在其中输入2个不同的密码,则会刷新屏幕上有一个空表格,没有错误。 Also I have tried sending the form.errors through the django messages and it passes the correct error when there is one but this solution is not practical for me. 我也尝试过通过django消息发送form.errors,当存在错误时它将传递正确的错误,但是这种解决方案对我来说不切实际。

It wont let me post the template because of indenting. 由于缩进,它不会让我发布模板。 I am using {{form.non_field_errors}} at the top of the form and then {{ form.email.error }} etc. 我在表单顶部使用{{form.non_field_errors}},然后使用{{form.email.error}}等。

Please help if you can:) 如果可以的话请帮忙:)

Form class.. 表格类

class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)

class Meta:
    model = User
    fields=('username', 'email', 'password1', 'password2')

def save(self, commit=True):
    User= super(MyRegistrationForm, self).save(commit=False)
    User.email = self.cleaned_data["email"]
    if commit:
        User.save()

    return User

View method... 查看方法...

def home(request):
args = {}
args.update(csrf(request))
if request.method =='POST':
    form = MyRegistrationForm(request.POST)
    if form.is_valid():
        ##save_it = form.save(commit=False)
        form.save()
        messages.success(request, 'Thank you for joining!')
        #return HttpResponseRedirect('thank-you')

        return render_to_response('thankyou.html', locals(), context_instance=RequestContext(request))
    else:
        args['form'] = MyRegistrationForm()  
        return render_to_response('signup.html', args, context_instance=RequestContext(request))
args={}
args.update(csrf(request))
args['form'] = MyRegistrationForm()

context = RequestContext(request,
                       {'user': request.user})
return render_to_response('signup.html', args,
                         context_instance=context)

You are explicitly recreating the form if it has errors, replacing it in the context with one that is unbound and therefore doesn't have errors. 如果表单有错误,您将明确地重新创建该表单,并在上下文中将其替换为未绑定的表单,因此没有错误。 Don't do that. 不要那样做 Your view should be simply: 您的看法应该很简单:

def home(request):
    if request.method =='POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Thank you for joining!')
            return HttpResponseRedirect('thank-you')

    else:
        form = MyRegistrationForm()

    return render(request, 'signup.html', {'form': form})

Note the other changes: always redirect after a successful post; 注意其他更改:成功发布后始终重定向; render rather than render_to_response, since it creates a RequestContext for you; render而不是render_to_response,因为它会为您创建一个RequestContext; and no need to add the user or csrf values yourself, since they are added by the context processors as long as you do use a RequestContext. 不需要自己添加用户或csrf值,因为只要您确实使用RequestContext,它们就会由上下文处理器添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM