简体   繁体   English

使用带有 Django.contrib.auth 视图的应用程序名称

[英]Using App name with Django.contrib.auth views

I am new to Django and I am creating a user change password page.我是 Django 的新手,我正在创建用户更改密码页面。 However, I keep encountering a NoReverseMatch error which I suspect is due to my app name but I am not able to resolve it even after spending hours googling for a solution.但是,我一直遇到NoReverseMatch错误,我怀疑这是由于我的app name造成的,但即使在花费数小时谷歌搜索解决方案后,我也无法解决它。

My urls.py file:我的 urls.py 文件:

from os import name
from django.urls import path
from account import views


from django.contrib.auth import views as auth_views     # Import Django built-in authentication views



app_name    = 'account'

urlpatterns = [
    path('test/', views.test_login, name='test'),
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
    path('password_change/', auth_views.PasswordChangeView.as_view(), name='password_change'),

]

My settings.py:我的设置.py:

# Login Logic
LOGIN_REDIRECT_URL      = 'account:test'    # Tells Django which URL to redirect user to after a successful login if no next parameter is present in the request
LOGIN_URL               = 'account:login'        # The URL to redirect the user to log in - based on the name in the urls.py
LOGOUT_URL              = 'account:logout'       # The URL to redirect the user to log out - based on the name in the urls.py

my html file我的 html 文件

{% extends "base.html" %}

{# This is the template to allow user to change their password #}

{% block title %}Change Your Password{% endblock title %}

{% block content %}

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-8">
                <form action="{% url 'account:password_change_done' %}" class="form-signin" method="post">
                {% csrf_token %}

                    <h1 class="h3 mb-3 font-weight-normal text-center">Change your password</h1>

                    <div class="form-group">
                        <label for="old_password">Old Password</label>
                        <input class="form-control" type="password" required id="id_old_password" name="old_password" autocomplete="current-password" placeholder="Old Password" autofocus>
                    </div>

                    <div class="form-group">
                        <label for="new_password1">New Password</label>
                        <input class="form-control" type="password" required id="id_new_password1" name="new_password1" autocomplete="new-password" placeholder="New Password">
                    </div>

                    <div class="form-group">
                        <label for="new_password2">Confirm Password</label>
                        <input class="form-control" type="password" required id="id_new_password2" name="new_password2" autocomplete="new-password" placeholder="Confirm Password">
                    </div>

                    <small class="form-text text-muted">
                        {% if form.new_password1.help_text %}
                            {{ form.new_password1.help_text|safe }}
                        {% endif %}
                    </small>

                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>

                {% if form.errors %}
                    {% for field in form %}
                        {% for error in field.errors %}
                            <div class="alert alert-danger">
                                <strong>{{ error|escape }}</strong>
                            </div>
                        {% endfor %}
                    {% endfor %}
                    {% for error in form.non_field_errors %}
                        <div class="alert alert-danger">
                            <strong>{{ error|escape }}</strong>
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
        </div>
    </div>
{% endblock content %}

Much help is appreciated.非常感谢您的帮助。

Have added in the html file for referece已在 html 文件中添加以供参考

A "NoReverseMatch" error means, Django is not being able to process and see the exact url being passed. “NoReverseMatch”错误意味着 Django 无法处理并查看正在传递的确切 url。 Including the "app_name" has said by most Django devs makes the code neat and easy to streamline to as to which url you want and most importantly helps to avoid duplicating urls..包括大多数 Django 开发人员所说的“app_name”使代码整洁且易于简化至您想要哪个 url,最重要的是有助于避免重复 url。

An instance will be having this url path of "home" in your app accounts and also having that same path "home" in some other app, the use of specifying the app_name will help Django, include the urls to their respective apps so you know which requires which.一个实例将在您的应用帐户中具有“home”的 url 路径,并且在其他一些应用程序中也具有相同的路径“home”,使用指定 app_name 将有助于 Django,包括各自应用程序的 URL,以便您知道这需要哪个。

Your urls are correct to me and since it's a NoReverseMatch error, it will have to be your at your html or view or anywhere where you are calling a url.你的网址对我来说是正确的,因为它是一个 NoReverseMatch 错误,它必须是你的 html 或查看或任何你调用 url 的地方。

Try including the app name in either your html and view on where you are calling the url.. Something like this尝试在您的 html 中包含应用程序名称,并查看您在哪里调用 url .. 像这样

{% app_name:name_url %} And same to the view app_name:name_url {% app_name:name_url %} 与视图 app_name:name_url 相同

Always make sure there is semi-colon between the app_name and the url... As it will help you tell Django as to which url from which app!!始终确保 app_name 和 url...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM