made changed middleware and made necessary changes to settings and urls fles so that the user is redirected to their desired page after a login redirect unless the desired page was logout, in which case the user will be redirect

This commit is contained in:
Andy Seracuse 2020-05-03 14:11:57 -06:00
parent c5166ec845
commit 85c948ef67
3 changed files with 10 additions and 2 deletions

View File

@ -1,6 +1,9 @@
from django.shortcuts import HttpResponseRedirect
from django.urls import reverse_lazy
from django.db import connection
from django.shortcuts import redirect
from django.conf import settings
from django.shortcuts import redirect
import logging
import time
import operator
@ -63,8 +66,10 @@ class AuthRequiredMiddleware(object):
# No authorization was found for the request
if not authorized:
if not request.path_info == reverse_lazy('login') and not request.path_info.startswith('/api/'):
if request.path_info == reverse_lazy('logout'):
return HttpResponseRedirect(reverse_lazy('login'))
if not request.path_info == reverse_lazy('login') and not request.path_info.startswith('/api/'):
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
# Code to be executed for each request/response after
# the view is called.

View File

@ -39,6 +39,9 @@ else:
with open(cfg_filename, 'r') as cfg:
CONFIG = yaml.safe_load(cfg)
#provide a default login url
LOGIN_URL = "/login"
# Read the autogenerated key-file
key_file = open(os.path.join(BASE_DIR, 'secret_key.txt'), 'r')

View File

@ -89,7 +89,7 @@ urlpatterns = [
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^login/', auth_views.LoginView.as_view(), name='login'),
url(r'^login/?', auth_views.LoginView.as_view(), name='login'),
url(r'^logout/', auth_views.LogoutView.as_view(template_name='registration/logout.html'), name='logout'),
url(r'^settings/', include(settings_urls)),