Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with ERR_TOO_MANY_REDIRECTS django 2.1

I started to create login module in django. Login module is ok but I have problem with logout. When i click Logout - we see "error -ERR_TOO_MANY_REDIRECTS"

Probably something in this file is incorect: account/urls.py

from django.conf.urls import url
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views

app_name = 'account'

urlpatterns = [
    path('', auth_views.LoginView.as_view(template_name='account/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='registration/logout.html'), name='logout'),
    path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
    path('dashboard/', views.dashboard, name='dashboard'),

base.html

<body>
  <div id="header">
  {% if request.user.is_authenticated %}
    <ul class="menu">
      <li {% if section == "dashboard" %} class="selected"{% endif %}>
        <a href="{% url "account:dashboard" %}">Panel główny</a>
      </li>
      <li {% if section == "images" %} class="selected"{% endif %}>
        <a href="#">Obrazy</a>
      </li>
      <li {% if section == "people" %} class="selected"{% endif %}>
        <a href="#">Ludzie</a>
      </li>
    </ul>
  {% endif %}

    <span class="user">
        {% if request.user.is_authenticated %}
            Witaj, {{ request.user.first_name }}
            <a href="{% url "account:logout" %}">Wyloguj</a>
        {% else %}
            <a href="{% url "account:login" %}">Zaloguj</a>
        {% endif %}
    </span>
    </div>
    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
</body>

logout.html

{% extends "base.html" %}

{% block title %} Wylogowanie {% endblock %}

{% block content %}
    <h1>Wylogowanie</h1>
    <p>Zostales wylogowany. Mozesz
        <a href="{% url "account:login" %}">zalogowac sie ponownie</a></p>
{% endblock %}

settings.html

...
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_REDIRECT_URL = reverse_lazy('account:logout')

show error

like image 310
Martix Avatar asked Dec 12 '18 20:12

Martix


People also ask

What causes Err_too_many_redirects?

The ERR_TOO_MANY_REDIRECTS error indicates that the browser is stuck in an infinite redirection loop. An infinite redirection loop happens when you visit a URL pointing to another URL, which points back to the first one.

What is a redirect error?

Last updated: 2 March 2022. A “too many redirects” error happens when website traffic is redirected in such a way that a page can never be displayed. This error is almost always the result of conflicting redirect rules.

How do I redirect a return in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .


1 Answers

You've set LOGOUT_REDIRECT_URL to point back to the LogoutView which will cause a redirect loop. The LOGOUT_REDIRECT_URL should point to a URL that the user will be redirected to after they've logged out using the LogoutView.

Setting LOGOUT_REDIRECT_URL will override any template that's been set. Since you've explicitly set a template for the LogoutView in your urls.py, you should remove LOGOUT_REDIRECT_URL from your settings which will allow the template to be rendered.

like image 159
Will Keeling Avatar answered Oct 04 '22 01:10

Will Keeling