简体   繁体   中英

How deny access to the previous page when a user have logged out in django?

I'm a newbie in django, im using the 1.8 version in a Ubuntu server, i made the log in and logout process successfully but when my users try to go to the previous page, they can see it as an anonymous user. How can i deny the access?

I tried to use a if on my views.py but doesn't work:

def login(request):
    if not request.user.is_authenticated():
        return render(request,"base.html",{})
    return render(request,"general.html",{})

@login_required 
def general(request):
    return render(request,"general.html")

Hope someone can help me.

EDIT: I'm using the default urls of django to login and log out.

url(r'^$','django.contrib.auth.views.login', {'template_name': 'base.html'}, name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': 'login'}, name='logout'),

You can hide the template by using,

{% if user.is_authenticated %}
<html></html>
{% else %}
<html> add for logged out user</html>
{% endif %}

This will prevent the users to see your page if they hit back button after logged out.

Edit: In your logout function you can use this,

from django.views.decorators.cache import cache_control

@cache_control(no_cache=True, must_revalidate=True)
 def your_function()
   #add your code
 return

This will clear cache after a user logged out.

I solved it using a simple script that makes a reload at the moment when the user logs out that "clear" the cache, probably that's not the proper way to do it but works for me, i'll try to keep searching a better way to do it. If anyone knows it, please share.

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