there is an issue in Django with the LogoutView. when I try to enter the link 'accounts/logout' django server pops in the cmd:
Method Not Allowed (GET): /accounts/logout/
Method Not Allowed: /accounts/logout/
[24/Feb/2024 13:48:11] "GET /accounts/logout/ HTTP/1.1" 405 0
this is the view file content:
from django.urls import path, include from django.contrib.auth import views as auth_views from .views import showProfile, logout_user
app_name = 'user'
urlpatterns = [
path('logout/', auth_views.LogoutView.as_view(template_name='registration/logged_out.html'), name='logout'),
path('', include('django.contrib.auth.urls')),
]
and this is the 'registration/logged_out.html' file content:
{% extends "generic_base.html" %}
{% block content %}
<form method="post" action="{% url 'user:logout' %}">
{% csrf_token %}
<button type="submit">Logout</button>
</form>
{% endblock content %}
the templates file is located in the application 'accounts'. and the app_name = 'user'
and this is the generic_base.html template content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block title %}
<title>Donations</title>
{% endblock title %}
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>
I tried a lot of ways to fix this issue, but none of them worked.
I also saw all the provided solutions in Stack Overflow about this problem, but none of them worked. I also tried the solutions in Django built in Logout view Method Not Allowed (GET): /users/logout/
but this didn't solve the problem.
A LogoutView
mainly has a template_name
because it subclasses from the TemplateView
yes, but that is only used if somehow the redirect points back to the LogoutView
itself, but that is probably a bad idea anyway.
You thus don't use the LogoutView
to show a form to logout, you put the logout button on some other view, or on all pages, and then thus logout there with the form you provided in the question. You thus can put this form for example in the navbar, it will logout the user and redirect to the page specified by the next_page
if that is specified in the LogoutView
; or the LOGOUT_REDIRECT_URL
setting [Django-doc] if that one is not specified.