使用参数'()'和找不到关键字参数'{}来反向'login'。尝试过0种模式:[]

时间:2015-12-15 16:22:51

标签: python django django-urls

我正在尝试使用django登录视图实现登录页面,这里是代码:

urls.py

urlpatterns = patterns(
    '',
    url(r'^login/$', 'django.contrib.auth.views.login'),
    url(r'^logout/$', 'django.contrib.auth.views.logout'),
)

和模板:

login.html

{% extends 'base_cost_control.html' %}
{% block contentbase %}

{% if form.errors %}
    <p>Invalid user or password</p>
{% endif %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
    {% csrf_token %}
    <div class="row" align="center">
    <br>
    <br>
    <h3> CONTROL DE COSTOS </h3>
    <br><br>
    <table>
        <tr>
            <td width=700px>{%include "partials/field.html" with field=form.username %}</td>
        </tr>
        <tr>
            <td width=700px>{%include "partials/field.html" with field=form.password %}</td>
        </tr>
        <tr>
            <td align="right"><input type="submit"  class="btn btn-info" name = "siguiente" id="siguiente"          value="Ingresar"/></td>
        </tr>
    </table>
    </div>

</form>

{% endblock contentbase %}

所以,我知道django.contrib,auth.views.login生成视图,我只需创建login.html模板,但我在login.html的第8行收到此错误:

Reverse for 'login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我不知道该怎么做......

1 个答案:

答案 0 :(得分:10)

您尚未命名登录网址,因此您无法通过名称login撤消该网址。只需将名称添加到模式中:

from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^login/$', auth_views.login, name='login'),
    url(r'^logout/$', auth_views.logout, name='logout'),
]