tutorial-extensions icon indicating copy to clipboard operation
tutorial-extensions copied to clipboard

'Homework: Adding security to your website' section does not work on newer versions of django

Open coarsecrunch opened this issue 1 year ago • 0 comments

This code (taken from the tutorial listed in the title), no longer works for the logout button. According to the Django update from 4.1 onward https://docs.djangoproject.com/en/4.1/releases/4.1/#log-out-via-get, trying to logout just from just the link does not work while using the builtin views.LogoutView anymore. I was able to fix mine by just switching the logout link/button to a form.

E.G. from:

<div class="page-header">
    {% if user.is_authenticated %}
        <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
        <a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
        <p class="top-menu">Hello {{ user.username }} <small>(<a href="{% url 'logout' %}">Log out</a>)</small></p>
    {% else %}
        <a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>
    {% endif %}
        <h1><a href="/">Django Girls Blog</a></h1>
</div>

to:

<div class="page-header">
    {% if user.is_authenticated %}
        <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
        <a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
        <form method="POST" action="{% url 'logout' %}">
             {% csrf_token %}
             <button type="submit" class="btn btn-danger">Log out</button>
       </form>
    {% else %}
        <a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>
    {% endif %}
    <h1><a href="/">Django Girls Blog</a></h1>
</div>

coarsecrunch avatar Jan 17 '25 15:01 coarsecrunch