Django登录重定向到密码重置页面,而不是个人资料页面

时间:2019-06-24 00:57:38

标签: python django django-authentication django-login

我是django的新手,正在构建我的第一个应用程序。在此阶段,我尝试使用django登录而不使用自定义视图功能。不幸的是,在django管理页面中创建用户并尝试登录后,我发现登录按钮将我重定向到输入了无效链接的密码重置页面。我不知道登录是否失败或成功。 我一直在寻找许多类似的问题,但从未遇到过相同的情况。

url

from django.conf.urls.static import static

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

#Add Django site authentication urls (for login, logout, password management)
urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/login/', include('django.contrib.auth.urls'), name = 'login'),
    path('accounts/logout/', include('django.contrib.auth.urls'), name = 'logout'),
    path('accounts/password_change/', include('django.contrib.auth.urls'), name = 'password_change'),
    path('accounts/password_change/done/', include('django.contrib.auth.urls'), name = 'password_change_done'),
    path('accounts/password_reset/', include('django.contrib.auth.urls'), name = 'password_reset'),
    path('accounts/password_reset/done/', include('django.contrib.auth.urls'), name = 'password_reset_done'),
    path('accounts/reset/<uidb64>/<token>/', include('django.contrib.auth.urls'), name = 'password_reset_confirm'),
    path('accounts/reset/done/', include('django.contrib.auth.urls'),name='password_reset_complete')
]

html库

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    {% block title %}
    <title>Acceuil</title>
    {% endblock %}

    {% load static %}

    <link rel="stylesheet" type=text/css href="{% static 'css/bootstrap.css' %}">
    <link rel="stylesheet" href="{% static 'css/menu.css' %}">
    <link rel="stylesheet"  type=text/css href="{% static 'css/style.css' %}"/>
</head>

<body>
    <section id="global">

{% block nav%}
<!--
    <nav class="navbar navbar-dark bg-primary  navbar-expand-md">
        <div id = "navigation_div" class="container-fluid">
            <a class="navbar-brand" href="#">Monkyro</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mon_menu"
                    aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="mon_menu">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item"><a class="nav-link" href="{% url 'index'%}" >Acceuil</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'registerpage' %}" >S'inscrire </a></li>
                    <li class="nav-item"><a class="nav-link" href ="{% url 'doctorlistpage'%}">Docteurs</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'tariffpage' %}" >Tarifs</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'reviewpage' %}" >Avis</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'myloginpage' %}"  >Se connecter</a></li>
                </ul>
            </div>
        </div>
    </nav>
-->
{% endblock %}

        <section class="page_top">
            <div id="centre_1">
            <h3><b>MonKyro vous propose des soins chiropratiques sur mesure.</b></h3>,<br>
            </div>
        </section>

        <section id="page_center">

            {% block content %}

            {%endblock %}
        </section>

        <section id="page_footer">
            <footer>
                <p>Myapp copyright 2019</p>
            </footer>
        </section>
    </section>

<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>

</body>
</html>

html扩展名

{% extends "base_generic_lite01.html" %}

{% block nav %}
<nav class="navbar navbar-dark bg-primary  navbar-expand-md">
        <div id = "navigation_div" class="container-fluid">
            <a class="navbar-brand" href="#">Monkyro</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mon_menu"
                    aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="mon_menu">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item"><a class="nav-link" href="{% url 'index'%}" >Acceuil</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'registerpage' %}" >S'inscrire </a></li>
                    <li class="nav-item"><a class="nav-link" href ="{% url 'doctorlistpage'%}">Docteurs</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'tariffpage' %}" >Tarifs</a></li>
                    <li class="nav-item"><a class="nav-link" href= "{% url 'reviewpage' %}" >Avis</a></li>
                    <li class="nav-item active"><a class="nav-link" href= "{% url 'myloginpage' %}"  >Se connecter</a></li>
                </ul>
            </div>
        </div>
    </nav>
{% endblock%}

{% block content%}
{% if form.errors %}
  <p>Your username and password didn't match. Please try again.</p>
{% endif %}

{% if next %}
  {% if user.is_authenticated %}
    <p>Your account doesn't have access to this page. To proceed,
    please login with an account that has access.</p>
  {% else %}
    <p>Please login to see this page.</p>
  {% endif %}
{% endif %}

<form method="post" action="{% url 'login' %}" >
{% csrf_token %}
<table>

<tr>
  <td>{{ form.username.label_tag }}</td>
  <td>{{ form.username }}</td>
</tr>

<tr>
  <td>{{ form.password.label_tag }}</td>
  <td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{next}}" />
</form>

{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>

{% endblock %}

我希望在帐户/配置文件/页面上被重定向,但是我被重定向到密码重置页面。

感谢您考虑我的问题。

2 个答案:

答案 0 :(得分:1)

you need to add two lines in your Settings.py
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = '/' or name of your homepage where you want to redirect

答案 1 :(得分:0)

根据此documentation,如果您想使用django的身份验证默认视图,则只需输入以下内容即可:

urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')), 
    # You can remove rest of the paths from urlpatterns
]

现在,如果要覆盖任何特定的视图,则可以像这样覆盖默认视图:

from django.contrib.auth import views as auth_views


urlpatterns = [
    path(
        'accounts/login/',
        auth_views.LoginView.as_view(template_name='custom_template.html'),
    ),
]