didn't return an HttpResponse object. It retur

2020-05-06 17:39发布

When incorrectly registering a user to my site I get this following error:

ValueError at /accounts/register/ The view accounts.views.register didn't return an HttpResponse object. It returned None instead.

views.py:

def register(request):
   if request.method == 'POST':
      form = RegistrationForm(request.POST)
      if form.is_valid():
          form.save()
          return redirect('/accounts')

   else:
      form = RegistrationForm()

      args = {'form': form}
      return render(request, 'accounts/reg_form.html', args)

urls.py:

urlpatterns = [
  path('', views.home, name="home"),
  path('explore/', views.explore, name='explore'),
  path('login/', LoginView.as_view(template_name='accounts/login.html'), name='login'),
  path('logout/', LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
  path('register/', views.register, name='register'),
  path('profile/', views.view_profile, name='profile'),
  path('profile/edit/', views.edit_profile, name='profile-edit'),
  path('change-password/', views.change_password, name='change_password'),
  path('reset-password/', PasswordResetView.as_view(template_name='accounts/reset_password.html'),
     name='reset-password'),
  path('reset-password/done/', PasswordResetDoneView.as_view(), name='password_reset_done'),
  path('reset-password/confirm/<uidb64>/<token>/',
     PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
  path('reset-password/complete/', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

main/urls.py:

urlpatterns = [
   path('', views.login_redirect, name='login_redirect'),
   path('admin/', admin.site.urls),
   path('accounts/', include('accounts.urls')),

  ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

标签: python django
3条回答
家丑人穷心不美
2楼-- · 2020-05-06 18:13

Your return should be four space back, That means the return should not be under the else condition.

change the views.py from this:

else:
  form = RegistrationForm()

  args = {'form': form}
  return render(request, 'accounts/reg_form.html', args)

to this:

else:
  form = RegistrationForm()

  args = {'form': form}
return render(request, 'accounts/reg_form.html', args)
查看更多
家丑人穷心不美
3楼-- · 2020-05-06 18:18

you are not returning anything for the case form is invalid..

def register(request):
   if request.method == 'POST':
      form = RegistrationForm(request.POST)
      if form.is_valid():
          form.save()
          return redirect('/accounts')
      else: #new
          return render(request, 'accounts/reg_form.html',  {'errors':form.errors})#new

   else:
      form = RegistrationForm()

      args = {'form': form}
      return render(request, 'accounts/reg_form.html', args)

it will redirect to account/reg_form.html with errors generated through forms

查看更多
看我几分像从前
4楼-- · 2020-05-06 18:24

You should move the last two lines back one level.

else:
  form = RegistrationForm()

args = {'form': form}
return render(request, 'accounts/reg_form.html', args)
查看更多
登录 后发表回答