本文仅针对Generic View 中涉及的两个问题进行讨论。
书中原文.
1.list_detail
-
from django.conf.urls.defaults import *
-
from django.views.generic import list_detail
-
from mysite.books.models import Publisher
-
publisher_info = { "queryset" : Publisher.objects.all(), }
-
urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info) )
2.direct_to_template
-
from django.conf.urls.defaults import *
-
from django.views.generic.simple import direct_to_template
-
**from mysite.books.views import about_pages**
-
urlpatterns = patterns('', ('^about/$', direct_to_template, { 'template': 'about.html'}),
-
**('^about/(w+)/$', about_pages),** )
在pycharm编辑器中,总是出现如下的问题,并且存在warning.
Function-based generic views have been deprecated; use class-based views
instead
(基于函数的通用视图已经被废弃;使用基于类的视图代替)
from django.views.generic import list_detail
from django.views.generic.simple import direct_to_template
在官网 Django 1.4 documentation 中存在如下一段话:
Prior to Django 1.3, generic views were implemented as functions. The function-based implementation has been deprecated in favor of the class-based approach described here.
也就是说,在1.3以后的版本,支持基于视图的通用视图,但是貌似基于函数的也是可以使用的(前面例子中,基于函数的direct_to_template 是可以正常使用的)
Existing usage of function-based generic views should be replaced with their class-based analogs:
Old function-based generic view
|
New class-based generic view
|
django.views.generic.simple.direct_to_template
|
|
django.views.generic.simple.redirect_to
|
|
django.views.generic.list_detail.object_list
|
|
django.views.generic.list_detail.object_detail
|
|
django.views.generic.create_update.create_object
|
|
django.views.generic.create_update.update_object
|
|
django.views.generic.create_update.delete_object
|
|
django.views.generic.date_based.archive_index
|
|
django.views.generic.date_based.archive_year
|
|
django.views.generic.date_based.archive_month
|
|
django.views.generic.date_based.archive_week
|
|
django.views.generic.date_based.archive_day
|
|
django.views.generic.date_based.archive_today
|
|
django.views.generic.date_based.object_detail
|
|
那么这两段我们应该如何来改写呢,尤其是list_detail,我按照书中的方法并没有显示成功。
1.direct_to_template
-
from django.conf.urls.defaults import *
-
from django.views.generic.base import TemplateView
-
from mysite.books.views import about_pages
-
urlpatterns = patterns('',
-
url(r'^about/$', TemplateView.as_view(template_name='about.html'), #Update
-
('^about/(w+)/$', about_pages), )
2.这里的context_object_name,template_name都可以不填写,这样context默认为object_list.
template_name会按照model推演出publisher_list.html.
-
from django.conf.urls.defaults import *
-
from django.views.generic.list import ListView
-
from books.models import Publisher
-
urlpatterns = patterns('',
-
url(r'^$',ListView.as_view(model=Publisher,context_object_name='publisher_list',template_name='publisher_list.html'))
-
)
publisher_list.html
-
<head>
-
<title></title>
-
</head>
-
<body>
-
<ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul>
-
{{ now }}
-
</body>
-
</html>
阅读(10851) | 评论(1) | 转发(1) |