简体   繁体   中英

Why is Logout working as a GET Request and not a POST Request?

This is the html file

<a class="nav-link" href="{% url 'logout' %}">Logout</a>
<form action="{% url 'logout' %}" method="POST" id ="logout">
 {% csrf_token %}
<input type ="hidden">
 </form> 

This code in view.py.

def logout(request):
    if request.method == 'POST':
        print("post-request")
        auth.logout(request)
        return redirect('index')
    else:
        print(request.method)
        auth.logout(request)
        return redirect ('index')

Why is the Logout a GET request and not a POST request? I made some changes in projects based on the documentation and could not understand why you can be successfully logged out with a GET request. https://docs.djangoproject.com/en/3.0/topics/auth/default/

your Logout is in <a> tag that has a link, which sends GET request, and logout form is outside of that <a> tag. and you are currently using your link, so form is not in used for logout.

you may need something like this:

<form action="{% url 'logout' %}" method="POST" id ="logout" class="nav-link">
    {% csrf_token %}
    <input type="submit" value="Logout">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM