Вопрос

I want to use the

@login_required 

decorator for a view called

allUsers_page

, which is used when you go to the url

r'^users/allUsers$'

In my settings.py, I have

LOGIN_URL='/login'

so when a user goes to the

allUser_page

view (when he goes to the url

users/allUsers

, if the user is not signed into his account, it will redirect him to the login page. Now, in the login page, is there a way for me to find out if the user just went to the login page directly or if he was redirected to the login page by the

@login_required

decorator? Basically, I want my login page to be something like

{% if the user was redirected to this page by @login_required %}
    <p>Hi, you must log in in order to view the page you were trying to view.</p>
{% else %}
    <p>Welcome! Please sign in.</p>
<!-- Here is where the log in form is -->

NOTE: I am using the generic login view.

Это было полезно?

Решение

If the user user was redirected from another page, django will provide the next query string parameter, so that the user can be redirected back after signing in.

Add this to your settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request",
)

then, in the template, check if this parameter exists or not.

{% if 'next' in request.GET %}

Not tested, though.

See Auth docs

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top