1

我在基于类的视图中使用以下代码

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”上引发

这是一个堆栈,看看发生了什么:

-> 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。

4

2 回答 2

0

It's just a guess, but maybe i'm doing it the right way and i simply can ignore this warning, because the function I'm using does not rely on the function based views but simply they are in the extended import scope of the urlresolvers module.

于 2013-02-02T10:34:23.397 回答
0

这只是 Django 功能的自动警告,如果您计划迁移到不再支持此功能的较新 Django 版本,则不应使用该功能。

您可以在此处查看弃用计划

于 2013-02-02T10:11:22.617 回答