wsvincent / djangoforprofessionals

Source code for Django for Professionals 4.0
https://djangoforprofessionals.com/
MIT License
632 stars 267 forks source link

Ch6 logout not working on django 5 #311

Open CAMM961001 opened 2 weeks ago

CAMM961001 commented 2 weeks ago

I know this is not the proper channel fot this issue, but I'm not sure where else to go.

For reference I provide my installs, result of pip freezing django latest and psycopg2-binary latest (20240624):

asgiref==3.8.1 Django==5.0.6 psycopg2-binary==2.9.9 sqlparse==0.5.0 typing_extensions==4.12.2 tzdata==2024.1

I followed all the steps for user logout in ch6 but failled to logout. When I click logout there seems to be a problem with the LOGOUT_REDIRECT_URL = "home" because it remains redirecting to "accounts/logout".

I found that there is an issue regarding so which seems to be fixed, however I still cannot logout succesfully.

When I switched to the base repos requirements.txt, everything runs properly.

asgiref==3.5.2 Django==4.0.4 psycopg2-binary==2.9.3 sqlparse==0.4.2

HBarotov commented 2 weeks ago

Hi,

This book was originally written for Django ~=4.0.0 so there are some incompatibility issues here. In Django 5, they removed the support for logouts via GET methods. Most likely, you are getting HTTP 405: Method not allowed (GET).

Support for logging out via GET requests in the django.contrib.auth.views.LogoutView and django.contrib.auth.views.logout_then_login() is removed. Django Docs

The easiest way of solving this issue is to log out via POST, which you can do through forms.

Django 4

<a href="{% url 'logout' %}">Log Out</a>

Django 5

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

I hope this answer addresses the issue you are having.