0
votes

Got the following error on pressing Logout Button with the code at bottom-

ValueError at /logout The view app.views.logout didn't return an HttpResponse object. It returned None instead. Request Method: GET
Request URL: http://127.0.0.1:8000/logout

Django Version: 3.0.8

Exception Type: ValueError Exception Value:
The view app.views.logout didn't return an HttpResponse object. It returned None instead.

my code is

def logout(request):
    if request.method=="POST":
        auth.logout(request)
        return redirect('login')

my html is

<li class="nav-item mr-3">
                <form id="logout" method="POST">
                    {% csrf_token %}
                    <input type ="hidden">
                </form>
                <a class="nav-link" href="{% url 'logout'%}" onclick="javacript:document.getElementById('logout').submit()">
                    Logout
                </a>   
            </li>
1
it is obvious you have <a> tag with href inside form which is being triggered instead of formiklinac
Which version of Django you are using?anuragal

1 Answers

-1
votes

In your code

def logout(request):
    if request.method=="POST":
        auth.logout(request)
        return redirect('login')

No need for explicit redirect, as auth.logout can handle redirection after logout. Just set LOGOUT_REDIRECT_URL = "my_url" in settings.py

Alternatively, Same can be achieve in urls.py as well if you are using Django 2.2.x or higher, in urls.py import

from django.contrib.auth.views import LogoutView

then add following path in urlpatterns,

path('logout/', LogoutView.as_view(next_page=settings.LOGOUT_REDIRECT_URL), name='logout'),

Then you don't need any code in your views.py