Django 1.10.2 错误“NoReverseMatch at”,“未找到带有参数 '()' 和关键字参数 '' 的 'django.contrib.auth.views.login' 的反向。

Posted

技术标签:

【中文标题】Django 1.10.2 错误“NoReverseMatch at”,“未找到带有参数 \'()\' 和关键字参数 \'\' 的 \'django.contrib.auth.views.login\' 的反向。” [复制]【英文标题】:Django 1.10.2 error "NoReverseMatch at " ,"Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '' not found." [duplicate]Django 1.10.2 错误“NoReverseMatch at”,“未找到带有参数 '()' 和关键字参数 '' 的 'django.contrib.auth.views.login' 的反向。” [复制] 【发布时间】:2017-02-26 06:06:27 【问题描述】:

我是 python 和 Django 的新手。我遇到一个错误,完全不知道如何解决它。 任何帮助将不胜感激。

from django.shortcuts import render
# Create your views here.
#log/views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
# Create your views here.
# this login required decorator is to not allow to any
# view without authenticating
@login_required(login_url="login/")
def home(request):
     return render(request,"home.html")

urls.py中的代码是,

from django.conf.urls import include,url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from log.forms import LoginForm
urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'', include('log.urls')),
 url(r'^login/$', auth_views.login ,'template_name':   'login.html','authentication_form': LoginForm),
 url(r'^logout/$', auth_views.logout, 'next_page': '/login'),
 ]

login.html 中的代码是,

% extends 'base.html' %


% 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 %

<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Please Sign In</h3>
                </div>
                <div class="panel-body">
                    <form method="post" action="% url 'django.contrib.auth.views.login' %">
% csrf_token %

                        <p class="bs-component">
                            <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>
                        </p>
                        <p class="bs-component">
                            <center>
                                <input class="btn btn-success btn-sm" type="submit" value="login" />
                            </center>
                        </p>
                        <input type="hidden" name="next" value=" next " />
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

% endblock %

% block javascript %


<script>
% if not user.is_authenticated %
$("ul.nav.navbar-nav.navbar-right").css("display","none");
% endif %
</script>

% endblock %

希望有这么多的信息会做......

【问题讨论】:

【参考方案1】:

name 添加到登录url 模式

kwargs = 'template_name': 'login.html','authentication_form': LoginForm
...
url(r'^login/$', auth_views.login, kwargs=kwargs, name='login'),
#                                                  ^^^^

然后在你的模板中使用name

<form method="post" action="% url 'login' %">

【讨论】:

以上是关于Django 1.10.2 错误“NoReverseMatch at”,“未找到带有参数 '()' 和关键字参数 '' 的 'django.contrib.auth.views.login' 的反向。的主要内容,如果未能解决你的问题,请参考以下文章

Django:使用PyCharm创建django项目并发布到apache2.4

Django 和 AngularJS:如何显示来自 Django 调试错误消息的 Angular $http 错误

django 错误

django debug关闭之后网页错误

Django模板错误?

django 3和django通道中的SynchronousOnlyOperation错误