Log out via GET requests is deprecated and will be removed in Django 5.0

I get this warning while running tests with selenium and pytest. Here's the test method:

def test_registration(
    self, driver, live_server, valid_user_data, django_user_model
):
    selenium_signup(driver, live_server, valid_user_data, django_user_model)
    selenium_sign_in(driver, live_server, valid_user_data)
    page_source = driver.page_source.lower()
    assert 'sign in' not in page_source
    assert 'sign up' not in page_source
    assert 'sign out' in page_source
    driver.find_element(By.ID, 'btnGroupDrop1').click()
    driver.find_element(By.XPATH, '//*[contains(text(), "Sign out")]').click()
    assert 'sign in' in page_source
    assert 'sign up' in page_source

This is where the warning is generated:

driver.find_element(By.XPATH, '//*[contains(text(), "Sign out")]').click()

Here's the button:

<li><a class="dropdown-item" href={% url 'signout' %}>Sign out</a></li>

The view uses django LogoutView and here's how it's defined

from django.contrib.auth.views import LogoutView

urlpatterns = [
    path('signout/', LogoutView.as_view(), name='signout'),
    ...
]

What would be a clean way to fix this?

As the doc mentioned, you must make the HTTP POST API call. So, it would be best if you replaced the "hyperlink" with a "button" (or something similar that simulates the HTTP POST request).

I would go with s simple button implementation, as below.

# logout-confirmation.html

<form method="post" action="{% url 'logout' %}">
    {% csrf_token %}
    <button type="submit">Sign Out</button>
</form>
# views.py

class LogOutRenderView(TemplateView):
    template_name = "logout-confirmation.html"
#urls.py

from django.contrib.auth.views import LogoutView
from .vies import LogOutRenderView

urlpatterns = [
    path("logout-confirmation/", LogOutRenderView.as_view(), name="logout-confirmation"),
    path("logout/", LogoutView.as_view(), name="logout"),
    
]

Note: You can replace the hyperlink with the <form...> ... </form> block.

Back to Top