Большая проблема в django LoginView
в Django есть проблема с LogoutView. когда я пытаюсь ввести ссылку 'accounts/logout' django сервер выдает команду:
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
вот содержимое файла просмотра:
from django.urls import path, include из 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')),
]
а это содержимое файла 'registration/logged_out.html':
{% extends "generic_base.html" %}
{% block content %}
<form method="post" action="{% url 'user:logout' %}">
{% csrf_token %}
<button type="submit">Logout</button>
</form>
{% endblock content %}
файл шаблонов находится в приложении 'accounts'. и app_name = 'user'
а это содержимое шаблона generic_base.html:
<!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>
Я перепробовал множество способов решения этой проблемы, но ни один из них не помог.
Я также видел все предоставленные решения в Stack Overflow по этой проблеме, но ни одно из них не сработало. Я также попробовал решения в Django built in Logout view Method Not Allowed (GET): /users/logout/
, но это не решило проблему.
У LogoutView
в основном есть template_name
, потому что он подклассифицируется от TemplateView
да, но это используется только если каким-то образом перенаправление указывает обратно на сам LogoutView
, но это, вероятно, плохая идея в любом случае.
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.