IKAdmin
IKAdmin

Reputation: 195

Django logout not working - can't understand why

Hi guys hope someone can help me here. I am just starting to create a simple web app using django and I am confused why this isn't working.

views.py

from django.shortcuts import render, redirect
from django.contrib.auth import login, logout

def index(request):
    return render(request, "fittracker/main.html")
def login_view(request):
    pass
def logout_view(request):
    logout(request)
    return redirect("fittracker/main.html")
def signup(request):
    pass

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path("logout/", views.logout, name='logout')
]

I am getting this error logout error

I have tired looking at the official docs, and this should redirect, but I am not sure why it isn't

Upvotes: 2

Views: 640

Answers (2)

Chaitanya Vaddi
Chaitanya Vaddi

Reputation: 21

Also, just for the readers after Nov 2023. From your template, you cannot do:

<a href="{% url 'logout' %}>Logout</a> #won't work

GET method for /logout has been deprecated. check here

New Implementation:

<form method="post" action="{% url 'logout' %}">
    {% csrf_token %}
    <button type="submit">logout</button>
 </form>

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477814

The name of the view is logout_view, so it hould be views.logout_view:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('logout/', views.logout_view, name='logout')
]

Now you use the logout that you re-exported from the django.contrib.auth module.

Upvotes: 2

Related Questions