用户creationform验证不起作用django

发布于 2025-01-25 04:38:56 字数 430 浏览 3 评论 0原文

一切似乎都可以正常工作,但是如果表单没有验证,而不是获得HTML验证错误,我会得到 valueerror ar /register /:user.register不会返回httpresponse。它没有返回

我的代码:

if request.method == 'POST':
   form = UserCreationForm(request.POST)
   if form.is_valid():
      form.save()
      messages.success(request, 'Acount created!')
else:
   form = UserCreationForm():
   return render(request, 'users/register.html', {"form":form})


Everything seems to be working fine, but if the form doesn't validate, instead of getting HTML validation errors, I get ValueError ar /register/: The user.register didn't return an HTTPResponse. It returned none instead.

My code:

if request.method == 'POST':
   form = UserCreationForm(request.POST)
   if form.is_valid():
      form.save()
      messages.success(request, 'Acount created!')
else:
   form = UserCreationForm():
   return render(request, 'users/register.html', {"form":form})


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2025-02-01 04:38:56

通常,在每个API调用上返回响应,

例如

import json
from django.http import HttpResponse

def profile(request):
    data = {
        'name': 'Vitor',
        'location': 'Finland',
        'is_active': True,
        'count': 28
    }
    dump = json.dumps(data)
    return HttpResponse(dump, content_type='application/json')

尝试根据django的任何响应添加返回语句,根据您尝试返回的内容,

您需要在块上添加类似的响应

。 com/tutorial/2016/07/27/how-to return-json-json-jsoded-response.html#:%7E:text = text = after%20version%201.7%2c%20django%20counts,在%20 returning%20%20%20%20 response%之前20object“ rel =” nofollow noreferrer> https://simpleisbetterthancomplex.com/tutorial/2016/07/07/27/how-to-to-to-to-to-wo--return-json-json-conconded-response.html#:tmo- 2C%20DJANGO%20counts,在%20返回%20%20 response%20Object 之前。

usually response are returned on each api call

like

import json
from django.http import HttpResponse

def profile(request):
    data = {
        'name': 'Vitor',
        'location': 'Finland',
        'is_active': True,
        'count': 28
    }
    dump = json.dumps(data)
    return HttpResponse(dump, content_type='application/json')

try to add return statement with any of Response from django based on what you try to return

you need to add similar to this on if block

refer : https://simpleisbetterthancomplex.com/tutorial/2016/07/27/how-to-return-json-encoded-response.html#:~:text=Since%20version%201.7%2C%20Django%20counts,before%20returning%20the%20response%20object.

睫毛上残留的泪 2025-02-01 04:38:56
form = UserCreationForm()
if request.method == 'POST':
   form = UserCreationForm(request.POST)
   if form.is_valid():
      form.save()
      messages.success(request, 'Acount created!')
      return # render any template or redirect to any view you want after account creation
return render(request, 'users/register.html', {"form":form})
form = UserCreationForm()
if request.method == 'POST':
   form = UserCreationForm(request.POST)
   if form.is_valid():
      form.save()
      messages.success(request, 'Acount created!')
      return # render any template or redirect to any view you want after account creation
return render(request, 'users/register.html', {"form":form})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文