NoReverseMatch at / login /

时间:2018-02-14 11:57:03

标签: django django-urls

/ login /中的NoReverseMatch是我从控制台获取的错误,我感觉这与urls.py之间正在进行的链接有关但我无法弄清楚哪一部分是错的。任何帮助将不胜感激。

控制台的完整错误位于

之下
Reverse for 'django.contrib.auth.views.login' not found. 
'django.contrib.auth.views.login' is not a valid view function or pattern name.

urls.py

from django.contrib import admin
from polls import views as polls_views
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^calendar/', include('calendarium.urls')),
    url(r'^polls/', include('polls.urls')),
    url(r'^$', polls_views.home, name='home'),
    url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'next_page': 'login'}, name='logout'),
    url(r'^signup/$', polls_views.signup, name='signup'),
]

base.html文件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>{% block title %}OnNote{% endblock %}</title>
  </head>
  <body>
    <header>
      <h1>My Site</h1>
      {% if user.is_authenticated %}
        <a href="{% url 'logout' %}">logout</a>
      {% else %}
        <a href="{% url 'login' %}">login</a> / <a href="{% url 'signup' %}">signup</a>
      {% endif %}
      <hr>
    </header>
    <main>
      {% block content %}
      {% endblock %}
    </main>
  </body>
</html>

的login.html

{% extends 'base.html' %}

{% block title %}Login{% endblock %}

{% block content %}
    <div id="content-container" class="container p-none">
        <div class="lgn-container col-lg-8">
            <form id="login-form" method="post" 
                  action="{% url 'django.contrib.auth.views.login' %}">
                {% csrf_token %}
                <table class="table">
                    <tr>
                        <td><label for="id_username">Username</label></td>
                        <td><input id="id_username" name="username" 
                                   type="text" class="form-control"></td>
                    </tr>
                    <tr>
                        <td><label for="id_password">Password</label></td>
                        <td><input id="id_password" name="password" 
                                   type="password" class="form-control"></td>
                    </tr>
                </table>
                {% if form.errors %}
                    <p class=" label label-danger">
                        Your username and password didn't match. 
                        Please try again.
                    </p>
                {% endif %}

                <input type="submit" value="Login" 
                       class="btn btn-primary pull-right" />
                <input type="hidden" name="next" value="{{ next }}" />
            </form>
        </div>
    </div>
{% endblock content %}

有谁可以指出我在这里做错了什么?我认为这与我的urls.py有关但我无法弄明白什么,任何帮助都会非常感激

2 个答案:

答案 0 :(得分:1)

<form id="login-form" method="post" action="">

在login.html中

你不需要这个动作,只需保持它自己发布就行了

答案 1 :(得分:1)

您可以使用不执行操作的表单,也可以将{% url 'django.contrib.auth.views.login' %}更改为{% url 'login' %}

顺便说一句,不推荐使用标准登录视图,您将收到警告。 如果您想更改默认LoginView.as_view()(&#39; registration / login.html&#39;),最好使用template_name或将其子类化。