Django中的NoReverseMatch,名称空间为

时间:2015-05-26 19:38:25

标签: python django django-urls

我不能在没有抛出NoReverseMatch的情况下在Django中进行反转,即使没有反转也会加载页面。 以下是相关代码:

主/ urls.py

from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
    url(r'(^$)|(^admin/)',include('example_admin.urls',namespace='example_admin')),
)

example_admin / urls.py

from django.conf.urls import patterns, include, url
from example_admin import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
)

example_admin / views.py

from django.shortcuts import render
def index(request):
    context = {}
    return render(request, 'example_admin/index.hmtl', context)

一切正常,直到我把这一行放在我的index.html模板

<a href="{% url 'example_admin:index' %}">LNK</a>

我收到此错误: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'(^$)|(^admin/)$']

我也尝试过django shell,使用反向(&#39;索引&#39;)和反向(&#39; example_admin:index&#39;)并且都不适用于我。

1 个答案:

答案 0 :(得分:0)

我通过更改

中main / urls.py中的链接来实现它
url(r'(^$)|(^admin/)',include('example_admin.urls',namespace='example_admin')),

url(r'^admin/',include('example_admin.urls',namespace='example_admin')),
url(r'^$',include('example_admin.urls',namespace='example_admin')),

我想它真的不喜欢我的正则表达式OR语句,有谁知道为什么?