0

This is my logged_out.html

{% extends 'base.html' %} {% block title %} Logout {% endblock %} {% block content %} <form action="{% url 'login' %}", method="POST"> {% csrf_token %} <button type="submit">Logout</button> </form> {% endblock %} 

This is my urls.py file

 urlpatterns = [ ... path('accounts/', include("django.contrib.auth.urls")), path('accounts/logout/', auth_views.LogoutView.as_view(template_name='logged_out.html'), name='logout'), ] 

view.py

@require_POST def user_logout(request): logout(request) return render(request, "registration/logged_out.html", {}) 

I've followed the docs to make the /logout a POST and I still get 405 error saying it's a GET request. How do I solve this?

3
  • <form action="{% url 'login' %}", method="POST"> This form is confusing. The button says "Logout", but it sends you to the login url?? Commented Nov 3, 2024 at 20:07
  • Also, why is there a comma after action="...", ? That does not belong. Commented Nov 4, 2024 at 20:10
  • This question is similar to: Django built in Logout view `Method Not Allowed (GET): /users/logout/`. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 9, 2024 at 10:32

1 Answer 1

0

The issue that you are sending the POST request to the login form here:

<form action="{% url 'login' %}", method="POST"> 

it should be something like this: logged_out.html

{% extends 'base.html' %} {% block title %} Logout {% endblock %} {% block content %} <form action="{% url 'logout' %}", method="POST"> {% csrf_token %} <button type="submit">Logout</button> </form> {% endblock %} 

and the views.py to be:

def user_logout(request): if request.POST: logout(request) return redirect('login') return render(request, "registration/logged_out.html", {}) 

in this case if when the user accessing the logout page using GET request it show him Logout button when he clicks on it, it will logout him then redirect the user to the login page

Sign up to request clarification or add additional context in comments.

3 Comments

Applied this change, i still get Method Not Allowed: /accounts/logout/ "GET /accounts/logout/ HTTP/1.1" 405 0
the issue was naming the path "logout". i changed it to "loggout" and that seemed to work. really weird as i've seen other tutorials and posts accept "logout" as the url path
Weird, but glad to hear that it is solved

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.