-1

I was trying to create a blog app by following an online Django tutorial and while I was testing the sign-up page, I ran into a Value Error saying that the view did not return a HTTP response object. i tried everything but i could not find the answer as i am not a Django expert

in the users app's views.py file was the code that threw the error

from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages


def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request, f'Account Created for {username}')
            return redirect('blog-home')
    else:
        form = UserCreationForm()
        return render(request, 'users/register.html', {'form': form})

and this is the register template

{% extends "myblog/base.html" %}
{% block content %}
   <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                    <legend class="border-bottom mb-4">
                        Join Today!
                    </legend>
                    {{ form.as_p }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">
                    Sign Up!
                </button>
            </div>
        </form>
        <div class="border-top pt-3">
            <small class="text-muted">
                Already Have an account?
                <a href="#" class="ml-2">Sign In!</a>
            </small>
        </div>
   </div>
{% endblock content%}

And this is the file structure of the project File Structure

  • 2
    If we have a POST request, and the form is not valid, you do not return anything. – Willem Van Onsem Nov 24 '22 at 17:05
  • 2
    Does this answer your question? [Django "The view didn't return an HttpResponse object."](https://stackoverflow.com/questions/3085028/django-the-view-didnt-return-an-httpresponse-object) – Abdul Aziz Barkat Nov 24 '22 at 17:07

3 Answers3

0

If we have a POST request, and the form is not valid, you do not return anything. Unindent the render call, so:

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST, request.FILES)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request, f'Account Created for {username}')
            return redirect('blog-home')
    else:
        form = UserCreationForm()
    return render(request, 'users/register.html', {'form': form})
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

return something for the function register also , you are returning only in if else conditions

return render(request, 'users/register.html', {'form': form}) add this at the end of the function outiside if else (now we have returned something for the function register also , you are returning only in if else conditions

0

You can write function like this ...

def register(request):
    form = UserCreationForm()
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request, f'Account Created for {username}')
            return redirect('blog-home')
        else:
            form.errors
    context = {'form': form}
    return render(request, 'users/register.html', context)