我正在使用django的python我想在注销后点击后退按钮时重定向用户登录页面。怎么做到这一点?在哪里写代码?
要测试django管理员是否处理这个..我登录到django admin..log out,然后点击按钮,我能够看到上一页。为什么django admin不会处理这个问题。
这是django admin中注销的ccode:
def logout(request):
"""
Removes the authenticated user's ID from the request and flushes their
session data.
"""
request.session.flush()
if hasattr(request, 'user'):
from django.contrib.auth.models import AnonymousUser
request.user = AnonymousUser()
答案 0 :(得分:12)
终于找到了解决方案:
from django.views.decorators.cache import cache_control
@cache_control(no_cache=True, must_revalidate=True)
def func()
#some code
return
这将强制浏览器向服务器发出请求。
答案 1 :(得分:9)
您可能会发现需要在chrome中使用@cache_control(no_cache = True,must_revalidate = True,no_store = True)来完全停止任何后退按钮查看。
我在这里1
找到关键是chrome的no_store答案 2 :(得分:2)
在您退出并回击之后,您可以在管理页面上显示您不是真实页面的原因。而是在浏览器缓存中看到它的副本。
试试这个:
现在,您将被重定向到管理员后端的登录页面。
答案 3 :(得分:2)
+1 Digital Cake的回答!这解决了在FireFox上注销后备份到缓存页面的问题。我试过了:
@cache_control(no_cache = True,must_revalidate = True)
我的观点没有运气。尝试过Per Digital Cake:
@cache_control(no_cache = True,must_revalidate = True,no_store = True)
现在Firefox备份到登录屏幕。
答案 4 :(得分:2)
我知道这是一个老问题,但接受的答案对我不起作用。我遇到了同样的问题(使用django 1.8& Chrome)
最后,我找到了docs(django 1.7或更高版本)的解决方案。这肯定会有用。
请看下面的代码
from django.contrib.auth.decorators import login_required
@login_required(login_url='/login/')
def myview(request):
return HttpResponse(render(request,'path_to_your_view.html'))
@login_required
装饰器用于处理问题。您可以在doc
答案 5 :(得分:0)
这取决于您使用的是哪种身份验证系统。如果您正在使用某种类型的impl,您可以编写自己的Middleware
类,将未经身份验证的用户重定向到登录页面。
如果您使用的是某些lib,请检查其文档如何处理未经身份验证的用户对安全页面的请求。
答案 6 :(得分:0)
这是一个缓存问题。
您可以使用 cache_control 装饰器强制不缓存视图:
from django.views.decorators.cache import cache_control
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
def func()
# some code
return
这将强制浏览器向服务器发出请求。
更多关于cache_control