Chinaunix首页 | 论坛 | 博客
  • 博客访问: 487302
  • 博文数量: 80
  • 博客积分: 2393
  • 博客等级: 大尉
  • 技术积分: 1434
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-03 21:46
个人简介

己所不欲勿施于人!

文章分类

全部博文(80)

文章存档

2017年(1)

2016年(9)

2014年(1)

2013年(17)

2012年(5)

2011年(13)

2010年(9)

2009年(8)

2008年(17)

分类: Python/Ruby

2013-05-04 00:58:47

本文仅针对Generic View 中涉及的两个问题进行讨论。

书中原文.
1.list_detail

点击(此处)折叠或打开

  1. from django.conf.urls.defaults import *
  2. from django.views.generic import list_detail
  3. from mysite.books.models import Publisher
  4. publisher_info = { "queryset" : Publisher.objects.all(), }
  5. urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info) )
2.direct_to_template

点击(此处)折叠或打开

  1. from django.conf.urls.defaults import *
  2. from django.views.generic.simple import direct_to_template
  3. **from mysite.books.views import about_pages**
  4. urlpatterns = patterns('', ('^about/$', direct_to_template, { 'template': 'about.html'}),
  5. **('^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 

点击(此处)折叠或打开

  1. from django.conf.urls.defaults import * 
  2. from django.views.generic.base import TemplateView
  3. from mysite.books.views import about_pages 
  4. urlpatterns = patterns('', 
  5.              url(r'^about/$', TemplateView.as_view(template_name='about.html'), #Update
  6.              ('^about/(w+)/$', about_pages), )
2.这里的context_object_name,template_name都可以不填写,这样context默认为object_list.
 template_name会按照model推演出publisher_list.html.

点击(此处)折叠或打开

  1. from django.conf.urls.defaults import * 
  2. from django.views.generic.list import ListView
  3. from books.models import Publisher 
  4. urlpatterns = patterns('', 
  5.               url(r'^$',ListView.as_view(model=Publisher,context_object_name='publisher_list',template_name='publisher_list.html'))
  6. )
publisher_list.html

点击(此处)折叠或打开

  1. <head>
  2.     <title></title>
  3. </head>
  4. <body>
  5. <ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul>
  6. {{ now }}
  7. </body>
  8. </html>


阅读(10231) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

network__yang2015-06-02 21:37:09

ListView instead list_detail 

After importing django.views.generic.list.ListView

You just need to change list_detail.object_detail to ListView.as_view()