Django网址 - 反对' url name'找不到

时间:2017-12-26 07:43:47

标签: django django-forms django-templates django-urls

我有一个名为polls的Django应用程序。 我试图在模板中使用表单,以便其操作调用另一个网址。 mysite / urls.py:

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url(r'^$', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

轮询/ urls.py:

from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
    url(r'^$', views.call_login, name='call_login'),
    url(r'^homepage/$', views.login, name='login'),
]

的login.html:

<form name="form" method="post" action="{% url 'login' %}" >
    {% csrf_token %}
    <input type="text" placeholder="username" name="user"><br>
    <input type="password" placeholder="password" name="password"><br>
    <input type="submit" value="Login" />
</form>

我得到的错误: NoReverseMatch:

  

反向登录&#39;未找到。 &#39;登录&#39;不是有效的视图功能或   模式名称。

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

更改主要网址格式

来自

urlpatterns = [
    url(r'^$', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

到这个

urlpatterns = [
    url(r'^', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

和模板表单

<form name="form" method="post" action="{% url 'polls:login' %}" >
    {% csrf_token %}
    <input type="text" placeholder="username" name="user"><br>
    <input type="password" placeholder="password" name="password"><br>
    <input type="submit" value="Login" />
</form>