确定用户是否在“ LogoutView”中通过了身份验证

时间:2019-06-23 22:33:36

标签: python django django-generic-views

无论您之前是否登录过,Django始终在访问127.0.0.1:8000/admin/logout时显示相同页面。

我要显示的是登出成功消息 (如果用户之前已通过身份验证 );并显示错误消息(如果用户未通过身份验证,然后尝试注销)。

我还需要在注销成功消息中包含用户的名字

我正在使用基于类的django.contrib.auth.views.LogoutView视图,像这样:

class SignoutView(LogoutView):
    template_name = "users/signout.html"

    def get_next_url(self):
        redirect_to = self.request.GET.get("next", "/")
        return redirect_to

这是模板:

{% extends "base.html" %}
{% block content %}
<h1>Sign out Page</h1>
<br>
{% if was_authenticated %}
<div class="alert alert-success" role="alert">
  <h4 class="alert-heading">You have logged out from your account {{first_name|capfirst}}!</h4>
  <p>Thanks for spending some quality time with my web site today.</p>
  <hr>
  <p class="mb-0" id="displayTimer"></p>
</div>
{% else %}
<div class="alert alert-danger" role="alert">
  <h4 class="alert-heading">Ooh no!</h4>
  <p>Looks like you are not logged in! So you can not log out! Cool yeah?</p>
</div>
{% endif %}
{% endblock %}


{% block js %}
{% if was_authenticated %}
<script type="text/javascript">
var count = 5;
var url = "{{redirect_address}}";
var countdown = setInterval(function() {
  $('#displayTimer').text("You will be redirected to the home page in " + count-- + " seconds...");
  if (count < 0) {
    $('#displayTimer').text("Redirecting....");
    clearInterval(countdown);
    $(location).attr("href", url);
  }
}, 1000);
</script>
{% endif %}
{% endblock %}

我向视图中添加了以下额外内容:

class SignoutView(LogoutView):
    template_name = "users/signout.html"

    def set_extra_context(self):
        return {
            'was_authenticated': self.request.user.is_authenticated,
            'first_name': self.request.user.first_name,
        }

    def get_next_url(self):
        redirect_to = self.request.GET.get("next", "/")
        return redirect_to

,但似乎该函数将在注销过程之后运行,因此was_authenticated始终为 False ,而first_name始终为没有

我知道如何使用基于基于功能的视图处理这种情况,但是我更喜欢使用基于类的视图(如果可能的话!)。

谢谢。

2 个答案:

答案 0 :(得分:1)

注销用户是dispatch方法中的第一步。因此,要从该用户捕获数据(例如名字),您必须重写此方法并捕获数据,然后再调用注销。

您可以做类似的事情

@method_decorator(never_cache)
def dispatch(self, request, *args, **kwargs):
    if request.user.is_authenticated:
        self.first_name = request.user.first_name
    return super().dispatch(request, *args, **kwargs)

源中的行可供参考; https://github.com/django/django/blob/master/django/contrib/auth/views.py#L116

答案 1 :(得分:0)

在python(> = 3.6)脚本中:像波纹管一样覆盖// for the Users controller set country and city first def filters = { all(controller: 'users', action: 'create') { before = { if (session.countryAndCity == null) { session.forwardToURI = request.forwardURI redirect(controller: "countries", action: "countryAndCity") } } after = {Map model -> } afterView = {Exception e -> } } } 方法并添加成功消息

dispatch

在模板中:

from django.contrib import messages

class SignoutView(LogoutView):
    template_name = "users/signout.html"

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            messages.success(request, f'{request.user.first_name} successfully logged out')
        else:
            messages.error(request, f'{request.user.first_name} your error message')
        return super().dispatch(request, *args, **kwargs)