我在基于类的视图中使用以下代码
from django.core.urlresolvers import reverse_lazy
url = reverse('url_name')
urls.py
如下所示
from django.conf.urls import patterns, url
from myapp import views
urlpatterns = patterns('',
url(r'^$', views.TestView.as_view(), name='url_name'),
)
这样做时我收到了弃用警告:
DeprecationWarning: Function-based generic views have been deprecated; use class-based views instead.
DeprecationWarning
警告会出现在'django / views / generic / simple.py:8`
上这是一个堆栈,看看有什么hapens:
-> return urlresolvers.reverse('url_name')
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/core/urlresolvers.py(476)reverse()
-> return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/core/urlresolvers.py(363)_reverse_with_prefix()
-> possibilities = self.reverse_dict.getlist(lookup_view)
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/core/urlresolvers.py(276)reverse_dict()
-> self._populate()
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/core/urlresolvers.py(265)_populate()
-> lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/core/urlresolvers.py(216)callback()
-> self._callback = get_callable(self._callback_str)
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/utils/functional.py(27)wrapper()
-> result = func(*args)
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/core/urlresolvers.py(92)get_callable()
-> lookup_view = getattr(import_module(mod_name), func_name)
/home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/utils/importlib.py(35)import_module()
-> __import__(name)
> /home/vagrant/.virtualenvs/wfp/lib/python2.5/site-packages/django/views/generic/simple.py(6)<module>()
-> import warnings
未来的做法是什么?我目前正在使用Django 1.4。
答案 0 :(得分:0)
这只是Django功能的自动警告,如果您计划迁移到不支持此版本的较新Django版本,则不应使用该警告。
您可以查看弃用计划here
答案 1 :(得分:0)
这只是一个猜测,但也许我正在以正确的方式做到这一点我只是可以忽略这个警告,因为我使用的函数不依赖于基于函数的视图,而只是它们在扩展的导入范围内urlresolvers模块。