python - ERR_TOO_MANY_REDIRECTS django 2.1 的问题

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

我开始在 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'),

基础.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 %}

设置.html

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

show error

最佳答案

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

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

关于python - ERR_TOO_MANY_REDIRECTS django 2.1 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53750727/

相关文章:

python - 如何格式化 Python 线图 Bokeh/matplotlib 日期时间和图形问题的日期时间

django - 不可压缩文件错误: 'scripts/app.js' isn't accessible via COMPRESS_URL ('http://my-bucket.s3-us-west-2.amazonaws.com/' ) and can't be compressed

python - 用于运行 Django 后端和 React 前端的 Makefile

python - 为什么我得到 : invalid syntax error on python3

python - 类变量和线程 - 意外的行为

python - 我的立体声校准正确吗?为什么我的三角测量不起作用?

python - Flask 的 Apache Superset 虚拟环境安装问题

django - 使用 Wagtail CMS 片段但隐藏在管理面板中

python - 替换/更改 python 集中的项目

python - 合并具有不同列名但定义相同的多个 CSV