Home > Software engineering >  The view users.views.view didn't return an HttpResponse object. It returned None instead
The view users.views.view didn't return an HttpResponse object. It returned None instead

Time:01-25

I'm making a to-do list web with my friend. So, I have created the register function with email authentication, and it works fin. I pushed it to the GitHub and my friend pulled it and tried it in his laptop.

But when he clicks "register" he got this error The view users.views.view didn't return an HttpResponse object. It returned None instead.

We literally have the same code since he pulled the code from my repository, we both user virtual environment, installing from the same requirements.txt, and we both uses WSL2.

This is the code
views.py

class UserRegister(CreateView):
    form_class = UserRegisterForm
    template_name = 'users/form_register.html'
    redirect_authenticated_user = True
    success_url = reverse_lazy('tasks')

    # Forbid logged in user to enter register page
    def get(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            return redirect('tasks')
        return super(UserRegister, self).get(*args, **kwargs)

    # Send email verification
    def post(self, request, *args, **kwargs):
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()

            current_site = get_current_site(request)
            subject = 'Activate your account'
            message = render_to_string('users/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject, message)
            return redirect('login')
        else:
            form = UserRegisterForm()

form_register.html

-- Register -->
        <section>
        <div >
        <h2>Register for an account</h2>
        <form method="POST">
            {% csrf_token %}
            <div >
                <input type="text" name="username" required placeholder="Username">
                <input type="email" name="email" required placeholder="Email">
                <input type="password" name="password1" required placeholder="Password">
                <input type="password" name="password2" required placeholder="Confirm password">
            </div>
        <div ><input type="submit" value="Register"></div>

CodePudding user response:

You need to always return some HttpResponse. If form is not valid, then you have nothing in return. Method self.form_invalid is returning such response with errors, so you should use it.

def post(self, request, *args, **kwargs):
    form = UserRegisterForm(request.POST)
    if form.is_valid():
        ...
        return redirect('login')
    else:
        return self.form_invalid(form)
  • Related