[damned-lies] Add a proper logout view



commit 83cd23e86b412f89cbcb9cf938252ee22b081438
Author: Claude Paroz <claude 2xlibre net>
Date:   Mon Apr 2 10:50:03 2018 +0200

    Add a proper logout view

 common/views.py                     |    8 ++------
 damnedlies/urls.py                  |    4 ++++
 templates/login/login_usermenu.html |    3 ++-
 3 files changed, 8 insertions(+), 7 deletions(-)
---
diff --git a/common/views.py b/common/views.py
index 52d23e4..45fdc0f 100644
--- a/common/views.py
+++ b/common/views.py
@@ -1,5 +1,5 @@
 from django.conf import settings
-from django.contrib.auth import login, authenticate, logout
+from django.contrib.auth import login, authenticate
 from django.contrib import messages
 from django.http import HttpResponseRedirect, Http404
 from django.shortcuts import render
@@ -49,11 +49,7 @@ def site_login(request):
         if request.POST.get('referer', None):
             referer = request.POST['referer']
 
-        if 'logout' in request.POST and request.POST['logout']:
-            logout(request)
-            messages.success(request, _("You have been logged out."))
-            return redirect(referer)
-        elif 'username' in request.POST:
+        if 'username' in request.POST:
             username = request.POST['username']
             password = request.POST['password']
             user = authenticate(username=username, password=password)
diff --git a/damnedlies/urls.py b/damnedlies/urls.py
index 6e54a4c..7943ef8 100644
--- a/damnedlies/urls.py
+++ b/damnedlies/urls.py
@@ -2,6 +2,7 @@ from django.conf.urls import include, url
 from django.conf import settings
 from django.contrib import admin
 from django.contrib.auth import views as auth_views
+from django.views.decorators.http import require_POST
 from django.views.generic import TemplateView
 from django.views.static import serve
 
@@ -19,6 +20,9 @@ urlpatterns = [
     url(r'^login/$',
         common_views.site_login,
         name='login'),
+    url(r'^logout/$',
+        require_POST(auth_views.LogoutView.as_view()),
+        name='logout'),
     url(r'^register/$',
         common_views.site_register,
         name='register'),
diff --git a/templates/login/login_usermenu.html b/templates/login/login_usermenu.html
index 596c17f..0e8bc9a 100644
--- a/templates/login/login_usermenu.html
+++ b/templates/login/login_usermenu.html
@@ -4,9 +4,10 @@
     <div class="row">
         <div class="col-sm-12">
             <a class="btn btn-link no-decoration" href="{% url 'person_detail_username' user.username %}">{% 
trans 'User Settings' %}</a>
-                <form action="{% url 'login' %}" method="post" class="form">
+                <form action="{% url 'logout' %}" method="post" class="form">
                     {% csrf_token %}
                     <input type="hidden" name="logout" value="1">
+                    <input type="hidden" name="next" value="{{ request.path }}">
                     <div class="form-group">
                         <button type="submit" class="btn btn-link">{% trans 'Log out' %}</button>
                     </div>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]