Django UserCreationForm不会在模板中呈现错误(Django UserCreationForm not rendering errors in template)

编程入门 行业动态 更新时间:2024-10-22 20:28:50
Django UserCreationForm不会在模板中呈现错误(Django UserCreationForm not rendering errors in template)

我正在使用django的UserCreationForm来注册用户。 除错误外,它的工作原理完美。 我不能让他们渲染。 我不认为模板有什么问题,因为我用最基本的模板和使用form.as_p和form.as_table尝试了这个,但是注册仍然相同,但是如果你在其中放入2个不同的密码只是刷新屏幕空表,没有错误。 此外,我尝试通过django消息发送form.errors,并在有一个时传递正确的错误,但这个解决方案对我来说不实用。

由于缩进,它不会让我发布模板。 我在表单顶部使用{{form.non_field_errors}},然后使用{{form.email.error}}等。

如果可以的话请帮忙:)

表格类..

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

查看方法......

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)

I am using django's UserCreationForm to sign up users. 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. 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.

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.

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)

最满意答案

如果表单有错误,则显式重新创建表单,在上下文中将表单替换为未绑定表单,因此没有错误。 不要那样做。 您的观点应该简单:

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})

请注意其他更改:成功发布后始终重定向; render而不是render_to_response,因为它为您创建了一个RequestContext; 并且不需要自己添加用户或csrf值,因为只要您使用RequestContext,它们就会由上下文处理器添加。

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; 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.

更多推荐

本文发布于:2023-08-05 11:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   模板   错误   UserCreationForm   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!