4

I am creating a web application on Django. When a logged-in user clicks 'Logout', the logout page correctly appears. However, when clicking the back button in the browser, the user can once more re-enter the session. To fix this, I followed this post : Disable browser 'Back' button after logout? , and used cache_control. However, the user can still re-enter the 'closed' session by clicking the back button. Here's the relevant code:

views.py:

from django.views.decorators.cache import cache_control

@cache_control(no_cache=True, must_revalidate=True, no_store=True)
def logout_view(request):

#c={}
#c.update(csrf(request))
logout(request)
#request.session.flush()
#request.user = AnonymousUser
#Redirect to logout page
return render_to_response('gamestore/logout.html')

@cache_control(no_cache=True, must_revalidate=True, no_store=True)
def login_view(request):

    #do something

settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))
DATABASE_PATH = os.path.join(BASE_DIR, 'db.sqlite3')

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
#"/home/mukhera3/Desktop/wsdProject/gamestore/templates", #TODO use absolute path here

)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '[the-secret-key-needs-to-stay-secret]'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gamestore',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'wsdProject.urls'

WSGI_APPLICATION = 'wsdProject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DATABASE_PATH,
}
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

I am new to Django and Python coding, so may have made some basic errors. Please help

Community
  • 1
  • 1
user3033194
  • 1,775
  • 7
  • 42
  • 63

2 Answers2

2

I tried this solution and it worked for me. I put both @cache_control(no_cache=True, must_revalidate=True, no_store=True) and @login_required as sees in the code below.

remeber to import cashe control.

It does not work if I leave one of these out. They work together. Just see the code below

from django.contrib.auth.decorators import login_required
from django.views.decorators.cache import cache_control
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
@login_required(login_url='login')
def myview(request):
   return HttpResponse(render(request,'path_to_your_view.html'))

I am using django 2.1 and removed the forwardslashes in '/login/' and used 'login' instead

Mahmood
  • 31
  • 5
1

Yes, It does not work even after using "@cache_control"

I found the solution from the doc for django 1.7 or later.

Just see the code below

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 decorator is used to handle the issue. You can check more in doc.

Abhijeet Singh
  • 154
  • 1
  • 8