【问题标题】:Django NoReverseMatchDjango NoReverseMatch
【发布时间】:2014-02-10 00:24:36
【问题描述】:

我正在 django 1.6(和 python 2.7)中制作一个简单的登录应用程序,但在开始时出现错误,无法继续。

这是网站的 url.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
import login

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('login.urls', namespace='login')),
    url(r'^admin/', include(admin.site.urls)),
)

这是 login/urls.py:

from django.conf.urls import patterns, url
from login import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^auth/', views.auth, name='auth'),
)

这是登录/查看,py

from django.shortcuts import render
from django.contrib.auth import authenticate

def auth(request):
    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    if user is not None:
        # the password verified for the user
        if user.is_active:
            msg = "User is valid, active and authenticated"
        else:
            msg = "The password is valid, but the account has been disabled!"
    else:
        # the authentication system was unable to verify the username and password
        msg = "The username and password were incorrect."
    return render(request, 'login/authenticate.html', {'MESSAGE': msg})

def index(request):
    return render(request, 'login/login_form.html')

我有一个将其作为操作的表单:

{% url 'login:auth' %}

这就是问题所在,当我尝试加载页面时,我得到:

Reverse for 'auth' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$auth/']

但如果我将 url 模式设置为

url(r'', views.auth, name='auth')

它工作正常,只是它将动作设置为'/'。

我一直在寻找答案,但我不明白为什么它不起作用。

我尝试将登录 url 模式更改为 url(r'^login/$', include('login.urls', namespace='login')),但没有任何改变。

【问题讨论】:

标签: python django python-2.7 django-1.6


【解决方案1】:

问题在于您将身份验证 URL 包含在主 URL 中的方式。 因为您同时使用 ^ 和 $,所以只有空字符串匹配。放下$。

【讨论】:

  • 我不敢相信这很容易,我正在拔头发。谢谢!
  • 谢谢,这个答案让我至少省了一个小时的绝望 :)
  • 他们应该解决这个问题或创建更好的错误消息。我花了 20 分钟试图找出 WTF 出了问题。
猜你喜欢
  • 2017-05-21
  • 2013-09-26
  • 2013-01-19
  • 2016-07-20
  • 2017-04-18
  • 2015-07-18
  • 2016-11-09
相关资源
最近更新 更多