带有命名空间的 Django 中的 NoReverseMatch
NoReverseMatch in Django with namespace
我无法在不抛出 NoReverseMatch 的情况下在 Django 中执行反向操作,即使如果不执行反向操作,页面也会加载。
这是相关代码:
main/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 中尝试过,使用 reverse('index') 和 reverse('example_admin:index'),但都不适合我。
我通过将 main/urls.py 中的 link 从
更改为
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 语句,有人知道为什么吗?
我无法在不抛出 NoReverseMatch 的情况下在 Django 中执行反向操作,即使如果不执行反向操作,页面也会加载。 这是相关代码:
main/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 中尝试过,使用 reverse('index') 和 reverse('example_admin:index'),但都不适合我。
我通过将 main/urls.py 中的 link 从
更改为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 语句,有人知道为什么吗?