¿Cómo configurar dónde redirigir después de cerrar sesión en Django?


Me pregunto dónde puedo configurar la url para redirigirla después de cerrar sesión. Sé que puedes establecer la url de inicio de sesión. Quiero redirigir a mi página de inicio.

9 answers

Django moderno (2017+?) tiene una configuración llamada LOGOUT_REDIRECT_URL.

Djangos antiguos / Respuesta original

No es necesario sobrescribir o envolver nada.

De acuerdo con los documentos, solo puede proporcionar el argumento next_page a la vista de cierre de sesión. https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout

(r'^logout/$', 'django.contrib.auth.views.logout',
                          {'next_page': '/successfully_logged_out/'})
 140
Author: Yuji 'Tomita' Tomita,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-21 16:04:33

Una manera más fácil:

Agregue el parámetro 'siguiente' a su url de solicitud de cierre de sesión. Por ejemplo:

<a href="{% url 'auth_logout' %}?next=/path_to_the_page"> Logout</a>

Entonces la vista de cierre de sesión hará el truco por usted.

Para after-login-redirect, simplemente puede configurarlo en settings.py:

LOGIN_REDIRECT_URL = '/path_to_the_page'
LOGIN_URL = '/path_to_the_page'
 47
Author: YeRuizhi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-15 07:08:21

Desde Django 1.10, puede definir un LOGOUT_REDIRECT_URL (véanse los documentos )

 21
Author: edelans,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-05 23:16:44

Redirigir a la página actual

<a href="{% url 'logout' %}?next={{ request.path | urlencode }}">{% trans "Logout" %}</a>

Probado en Django 1.9.

Ver también: ¿Es posible pasar parámetros de consulta a través de la etiqueta de plantilla {% url%} de Django?

 6
Author: Ciro Santilli 新疆改造中心 六四事件 法轮功,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-25 20:10:36

En su vista de cierre de sesión, después de cerrar la sesión del usuario para siempre, devuelva HttpResponseRedirect(url). Por favor vea aquí para más detalles.

 3
Author: Ke Sun,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-15 17:04:38

Desde docs puede escribir su propia vista de cierre de sesión (que puede ser un simple envoltorio) sobreescribiendo la página 'siguiente'.

 2
Author: Don,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-15 16:55:29

Si desea establecer la URL de redirección a nivel de cliente, puede hacerlo en el urls.py:

(r'^management/logout/$', 'django.contrib.auth.views.logout'),

Y luego en la plantilla:

<a href="{% url 'django.contrib.auth.views.logout' %}?next=/">
    Log out
</a>

Donde el next, apunta a la URL derecha.

 2
Author: Menda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-22 18:03:21

Incluso puedes usar urls con nombre para tu siguiente parámetro:

<a href="{% url 'auth_logout' %}?next={% url 'homepage' %}"> Logout</a>
 1
Author: dangonfast,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-08 11:47:57

Puede redirigir al usuario a cualquier lugar utilizando LOGOUT_REDIRECT_URL en su setting.py file

LOGOUT_REDIRECT_URL = 'url name to redirect'
 1
Author: SACHIN CHAVAN,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-15 09:04:01