每当我尝试在中间件中使用 reverse() 时,在运行manage.py test
出现此类错误的测试时都会失败
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py", line 416, in _reverse_with_prefix
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'home' with arguments '()' and keyword arguments '{}' not found.
中间件.py
from django.core.urlresolvers import reverse
class TestMid (object):
def process_request(self, request):
target = reverse('home_custom')
网址.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'test_django.views.home', name='home_custom'),
)
视图.py
from django.http.response import HttpResponse
from django.core.urlresolvers import reverse
def home(request):
return HttpResponse('path to page %s' % reverse('home_custom'))
我没有写任何测试。我只是在运行 Django 附带的测试。
在 Django 中运行测试时,这看起来像一个类似于NoReverseMatch 错误的问题,但该问题没有得到解答。
Django 1.5.1 和 1.5.2