Django NoReverseMatch [英] Django NoReverseMatch

查看:176
本文介绍了Django NoReverseMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django 1.6(和python 2.7)中创建一个简单的登录应用程序,并且在开始的时候收到一个不允许我继续的错误。

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.conf.urls导入模式的网站的url.py

This is the site's 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:

And this is 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

This is login/views,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/']

但是如果我设置了url模式到

But if I set the url pattern to

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.

我尝试将登录网址格式更改为url(r' ^ login / $',include('login.urls',namespace ='login')),并没有改变任何东西。

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

推荐答案

问题在于您在主要内容中包含auth URL。
因为你使用^和$,只有空字符串匹配。删除$。

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 $.

这篇关于Django NoReverseMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆