Python Django NoReverseMatch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21240680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:23:12  来源:igfitidea点击:

Django NoReverseMatch

pythondjangopython-2.7django-1.6

提问by freakrho

I'm making a simple login app in django 1.6 (and python 2.7) and I get an error at the beggining that is not letting me continue.

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

This is the site's url.py

这是网站的 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)),
)

And this is login/urls.py:

这是登录/ 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'),
)

This is login/views,py

这是登录/查看次数,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')

I have a form that has this as action:

我有一个具有此操作的表单:

{% url 'login:auth' %}

And that's where the problem is, when I try to load the page, I get:

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

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

But if I set the url pattern to

但是如果我将 url 模式设置为

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

it works fine, only it sets the action as '/'.

它工作正常,只是它将操作设置为“/”。

I've been looking all around for an answer and I don't understand why it doesn't work.

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

I tried changing the login url pattern to url(r'^login/$', include('login.urls', namespace='login')), and it didn't change anything.

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

采纳答案by Daniel Roseman

The problem is in the way you include the auth URLs in the main one. Because you use both ^ and $, only the empty string matches. Drop the $.

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