【问题标题】:Problem with ERR_TOO_MANY_REDIRECTS django 2.1ERR_TOO_MANY_REDIRECTS django 2.1 的问题
【发布时间】:2018-12-12 20:18:19
【问题描述】:

我开始在 django 中创建登录模块。登录模块没问题,但我在注销时遇到问题。当我单击注销时 - 我们看到“错误 -ERR_TOO_MANY_REDIRECTS”

此文件中的某些内容可能不正确: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>

注销.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

【问题讨论】:

  • 您是否修改了注销视图?我怀疑这与您的 logout_then_login 视图有关,但必须查看更多代码。
  • 添加logout.html

标签: python django python-3.x django-2.1


【解决方案1】:

您已将LOGOUT_REDIRECT_URL 设置为指向LogoutView,这将导致重定向循环。 LOGOUT_REDIRECT_URLshould point to a URL,用户在使用LogoutView 注销后将被重定向到

设置LOGOUT_REDIRECT_URL 将覆盖任何已设置的模板。由于您已在 urls.py 中明确设置了 LogoutView 的模板,因此您应该从设置中删除 LOGOUT_REDIRECT_URL,这将允许渲染模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多