中间件中的reverse()调用在tes中失败

2024-04-26 13:02:59 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我尝试在中间件中使用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附带的测试。你知道吗

这看起来像是一个类似于NoReverseMatch error when running tests in Django的问题,但是这个问题没有得到回答。你知道吗

Django 1.5.1和1.5.2


Tags: 中间件djangoinfrompycoretestimport