Questions

Redirect to the same page from where login is requested in django-website

0

I am trying to authenticate before some pages can be accessed and i want the page to be redirected to the same previous page from where login was called. How can we do that?



asked 1 year, 10 months ago
Reputation: 1





1 Answer

0

To redirect to the same page from where login was called or required, each page with login link is required to know the current path and for that we need to add the request context preprocessor to settings.py. In most cases it should already be present but if it’s not there then make sure it’s added properly.

Settings.py

It should look something like the code below in settings.py.

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [ ],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },

        },
]

base.html

Now make the login link like this.

<a href="{% url 'account_login' %}?next={{request.path}}">Login</a>

In this account_login shows the relative url to the login page.

That’s it.

answered 1 year, 10 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  1 year, 10 months ago
Viewed:  2309 times
Active:  1 year, 10 months ago