确定用户是否在 "LogoutView" 中通过身份验证
Determine whether the user was authenticated in "LogoutView"
当您转到 127.0.0.1:8000/admin/logout
时,无论您之前是否登录,Django 总是显示 相同的 页面。
我想要的是仅显示 注销成功消息 如果用户之前已通过身份验证;如果用户未通过身份验证并尝试注销,则显示一条错误消息。
我还需要在注销成功消息中包含 用户的 first_name。
我正在使用基于 class 的 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
总是 None.
我知道如何使用 基于函数的视图 来处理这种情况,但我更喜欢使用基于 class 的视图(如果可能的话!)。
提前致谢。
注销用户是 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
在 python (>= 3.6) 脚本中:像下面那样覆盖 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)
在模板中:
{% if messages %}
{% for message in messages %}
<div class="alert temp-alert {% if message.tags %}alert-{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}danger{% else %}{{ message.tags }}{% endif %}{% endif %}" role="alert">{{ message }}</div>
{% endfor %}
{% endif %}
当您转到 127.0.0.1:8000/admin/logout
时,无论您之前是否登录,Django 总是显示 相同的 页面。
我想要的是仅显示 注销成功消息 如果用户之前已通过身份验证;如果用户未通过身份验证并尝试注销,则显示一条错误消息。
我还需要在注销成功消息中包含 用户的 first_name。
我正在使用基于 class 的 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
总是 None.
我知道如何使用 基于函数的视图 来处理这种情况,但我更喜欢使用基于 class 的视图(如果可能的话!)。
提前致谢。
注销用户是 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
在 python (>= 3.6) 脚本中:像下面那样覆盖 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)
在模板中:
{% if messages %}
{% for message in messages %}
<div class="alert temp-alert {% if message.tags %}alert-{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}danger{% else %}{{ message.tags }}{% endif %}{% endif %}" role="alert">{{ message }}</div>
{% endfor %}
{% endif %}