Cant sign into my Django project but i can sign up no problem "ValueError at /accounts/login/"

Viewed 47

Hey guys I'm just getting an error thrown up at me when I go to sign in on my Django project I was just wondering could anyone help shed some light as to what the error is. Ill include both my views and URLs below. I assume this is where the error must be but I'm unsure so apologies if I have not provided the right code. I will also include my templates. Repost

urls
    from django.urls import path
    from phoneshop import views
    from .views import signupView, signinView, signoutView

    urlpatterns = [
        path('create/', signupView, name='signup'),
        path('login/', signinView, name='signin'),
        path('logout/', signoutView, name='signout'),
    ]


 views
        from django.shortcuts import render, redirect
        from .forms import CustomUserCreationForm
        from .models import CustomUser
        from django.contrib.auth.models import Group
        from django.contrib.auth.forms import AuthenticationForm
        from django.contrib.auth import login, authenticate, logout
        
        def signupView(request):
            if request.method == 'POST':
                form = CustomUserCreationForm(request.POST)
                if form.is_valid():
                    form.save()
                    username = form.cleaned_data.get('username')
                    signup_user = CustomUser.objects.get(username=username)
                    customer_group = Group.objects.get(name='Customer')
                    customer_group.user_set.add(signup_user)
            else:
                form = CustomUserCreationForm()
            return render(request, 'signup.html', {'form':form})
        
        def signinView(request):
            if request.method == 'POST':
                form = AuthenticationForm(data=request.POST)
                if form.is_valid():
                    username = request.POST['username']
                    password = request.POST['password']
                    user = authenticate(username=username, password=password)
                    if user is not None:
                        login(request, user)
                        return redirect('phoneshop:allProdCat')
                    else:
                        return redirect('signup')
                else:
                    form = AuthenticationForm()
                return render(request, 'signin.html', {'form': form})
        
        def signoutView(request):
            logout(request)
            return redirect('signin')
 

templates

        {% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block metadescription %}
    Sign in and enjoy the variety of stunning and comfy cushions.
{% endblock %}
{% block title %}
    Login - Perfect Cushion Store
{% endblock %}
{% block content %}
    <div class="container">
        <br>
        <h1 class="my_title text-center">Login or Create a New Account</h1>
        <br>
        <div class="row">
            <div class="col-12 col-sm-12 col-md-12 col-lg-6 text-center my_custom_div">
                <br>
                <h2 class="my_title">New Customer?</h2>
                <br>
                <a href="{% url 'signup' %}" class="btn btn-secondary">Create New Account</a>
            </div>
            <div class="col-12 col-sm-12 col-md-12 col-lg-6 bg-light">
                <div>
                    <br>
                    <h2 class="my_title">Registered Customers Only</h2>
                    <form method="post">
                        {% csrf_token %}
                        <p>{{ form | crispy }}</p>
                        <button type="submit" class="btn btn-secondary">Login</button>
                    </form>
                    <br>
                </div>
                
            </div>
        </div>
        <br>
    </div>
    <br>
{% endblock %}

Error

Internal Server Error: /accounts/login/
Traceback (most recent call last):
  File "C:\Users\Aaron\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Aaron\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 186, in _get_response
    self.check_response(response, callback)
  File "C:\Users\Aaron\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 307, in check_response
    raise ValueError(
ValueError: The view accounts.views.signinView didn't return an HttpResponse object. It returned None instead.
[03/Mar/2021 18:03:40] "GET /accounts/login/ HTTP/1.1" 500 63674
Internal Server Error: /accounts/login/
Traceback (most recent call last):
  File "C:\Users\Aaron\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Aaron\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 186, in _get_response
    self.check_response(response, callback)
  File "C:\Users\Aaron\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 307, in check_response
    raise ValueError(
ValueError: The view accounts.views.signinView didn't return an HttpResponse object. It returned None instead.
[03/Mar/2021 18:03:57] "GET /accounts/login/ HTTP/1.1" 500 63674
1 Answers

You don't return a response in case of a get request in your view signinView. I believe this might have been an indentation mistake. Your view's code should be:

def signinView(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('phoneshop:allProdCat')
            else:
                return redirect('signup')
    else:
        form = AuthenticationForm()
    return render(request, 'signin.html', {'form': form})
Related