Search code examples
djangohttp-redirectdjango-rest-frameworkdjango-viewshttp-referer

Django: KeyError at /panel/home/login/ 'HTTP_REFERER'


I have a problem to get the HTTP_REFERER address from django. I want to userauth system in two separate login pages and for that, I set in one of login pages if the url that redirected person to this login page had 'business' word, redirect user into business login page. Thanks for Help

I wrote this Code:

if 'business' in request.META['HTTP_REFERER']:
return redirect('panel_business:login_business')

Here is My Error: KeyError at /panel/home/login/ 'HTTP_REFERER'

Django Error Full Trace:

Environment:


Request Method: GET
Request URL: http://localhost:8009/panel/home/login/?next=/panel/business/

Django Version: 4.2.6
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'website',
 'panel',
 'panel_business']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/arshia/w/recycle/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/arshia/w/recycle/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/arshia/w/recycle/panel/views.py", line 44, in login_home
    if 'business' in request.META['HTTP_REFERER']:

Exception Type: KeyError at /panel/home/login/
Exception Value: 'HTTP_REFERER'


Solution

  • OK, I found a simple way :)

    @login_required(login_url='your login url')
    def your_page(request):
        # something ...