Kaesekante
Kaesekante

Reputation: 109

Why do I get this valueerror when I try to validate my from via recaptcha?

I use django-nocaptcha-recaptcha and followed the exact steps in the documantation: https://github.com/ImaginaryLandscape/django-nocaptcha-recaptcha

This is my view:

def home(request):
    if request.method == 'POST':
        form = PostForm(request.POST or None)

        if form.is_valid():
            save_it = form.save(commit=False)
            save_it.save()
            return HttpResponseRedirect(reverse(view, args=(save_it.pk,)))

    else:
        form = PostForm(request.POST or None)
        return render(request, "home.html", locals())

I get this error message when I submit the form and the recaptcha remains unchecked:

The view posts.views.home didn't return an HttpResponse object. It returned None instead.

I hope there is no necessary information that I forgot. Any help would be appreciated

Upvotes: 1

Views: 36

Answers (2)

ilse2005
ilse2005

Reputation: 11429

You don't return a response when your form.is_valid() is False. Try adding this:

def home(request):
    if request.method == 'POST':
        form = PostForm(request.POST or None)

        if form.is_valid():
            save_it = form.save(commit=False)
            save_it.save()
            return HttpResponseRedirect(reverse(view, args=(save_it.pk,)))
        else:
            return render(request, "home.html", locals())  # new line
    else:
        form = PostForm(request.POST or None)
        return render(request, "home.html", locals())

Upvotes: 1

Kaesekante
Kaesekante

Reputation: 109

Right after I clicked send the solution occurred to me. I just needed to return the render of home.html in case the form is not valid. Sorry for the unnecessary post!

Upvotes: 1

Related Questions