Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2898988
  • 博文数量: 348
  • 博客积分: 2907
  • 博客等级: 中校
  • 技术积分: 2272
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 09:16
个人简介

专注 K8S研究

文章分类

全部博文(348)

文章存档

2019年(22)

2018年(57)

2016年(2)

2015年(27)

2014年(33)

2013年(190)

2011年(3)

2010年(14)

分类: Python/Ruby

2013-08-08 01:12:12

本文仅针对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>


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