我有一个全新的 Django 项目。我向 中添加了一个最小视图函数,向 中添加了views.py
一个 URL 模式urls.py
,通过函数引用而不是字符串传递视图:
# urls.py
# -------
# coding=utf-8
from django.conf.urls.defaults import *
from myapp import views
urlpatterns = patterns('',
url(r'^myview/$', views.myview),
)
# views.py
----------
# coding=utf-8
from django.http import HttpResponse
def myview(request):
return HttpResponse('MYVIEW LOL', content_type="text/plain")
我正在尝试reverse()
通过向其传递函数引用来获取 URL。但是我没有得到匹配,尽管我确认我传递给 reverse 的视图函数与我在 URL 模式中放入的视图函数完全相同:
>>> from django.core.urlresolvers import reverse
>>> import urls
>>> from myapp import views
>>> urls.urlpatterns[0].callback is views.myview
True
>>> reverse(views.myview)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 254, in reverse
*args, **kwargs)))
File "/Library/Python/2.5/site-packages/django/core/urlresolvers.py", line 243, in reverse
"arguments '%s' not found." % (lookup_view, args, kwargs))
NoReverseMatch: Reverse for '<function myview at 0x6fe6b0>' with arguments '()' and keyword arguments '{}' not found.
据我从文档中可以看出,函数引用在 URL 模式和reverse()
.
我正在使用 Django 主干,修订版 9092。