1
votes

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)
3
Try to import from django.http import HttpResponse and return with HttpResponseshaik moeed
if form.is_valid(): return redirect(...) else?Ivan Starostin

3 Answers

1
votes

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)
0
votes

You should move the last two lines back one level.

else:
  form = RegistrationForm()

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

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