您的位置:首页 > 编程语言 > Go语言

千与千寻django(六)---通用视图(Generic views)

2013-03-25 21:41 471 查看

通用视图的作用

视图提供了容易的接口来处理开发人员遇到的最常见的任务

所有的这些视图被用来在你的URL配置文件里创建配置字典并把这些字典作为参数传递给一个给定的模式 我们只需要使用这些内置的通用视图函数,而无需自己编写就可以实现相应的功能。

通用视图的一个例子

在urls.py中我们直接使用通用视图
from django.conf.urls.defaults import patterns, include, url
from mysite.views import *
from django.contrib import admin
from mysite.book.models import Book
from django.views.generic import ListView

admin.autodiscover()
urlpatterns = patterns('',
   (r'^helloword/$',helloword),
   (r'^templates/$',templates),
   (r'^admin/', include(admin.site.urls)),  
   (r'^book/list/$', ListView.as_view(
                  model=Book,
                  context_object_name='book_list', 
                  template_name='/book/book_list.html'                   
                                      )),
                    
)
这些代码的意思是使用Book模型(之前有创建这个模型)传过去为book_list然后路径为/book/book_list.html

好了我们写下book_list.html的代码吧如下:
<html>
 <head>
  <title>book_list</title>
  <body>
    <h1>books</h1>
    {%for book in book_list%}
        {{book.title}}
     {%endfor%}   
  </body>
 </head>
</html>
遍历模型然后打印出book的title

好了我们开始运行看看结果



恩,出来book的tiltle了,因为我就一条数据,所以就只有一个‘你好’,到此通用视图的一个小例子就举好了,当然通用视图还有好多其他的直接供我们使用
eg2:我们还可以以另外一方式写出以object_list为例:
<html>
 <head>
  <title>book_list</title>
  <body>
    <h1>b</h1>
    {%for book in a_list%}
        {{book.first_name}}
     {%endfor%}   
  </body>
 </head>
</html>


from django.conf.urls.defaults import patterns, include, url
from mysite.views import *
from django.contrib import admin
from mysite.book.models import Book, Author
from django.views.generic import ListView, list_detail

admin.autodiscover()
author_list_info = {
'queryset' : Author.objects.all(),
'template_object_name':'a',
}
urlpatterns = patterns('',
   (r'^helloword/$',helloword),
   (r'^templates/$',templates),
   (r'^admin/', include(admin.site.urls)),  
   (r'^book/list/$', ListView.as_view(
                  model=Book,
                  context_object_name='book_list', 
                  template_name='/book/book_list.html'                 
                                      
                                      )),
(r'authors/$', list_detail.object_list, author_list_info)         
)


其中queryset为必须参数,其他参数可以任选,所以我选了一个template_object_name,其他的参数包括



我们就可以使用它的context有:



注:如果不传递template_object_name他会自动匹配其源码如下:



其他的通用视图

django.views.generic.list_detail模块

object_list 显示模型对象列表

object_detail 显示单个模型对象

django.views.generic.create_update模块

create_object 创建模型对象

update_object 修改模型对象

delete_object 删除模型对象

django.views.generic.simple模块

direct_to_template 直接使用指定的模板渲染给定的context对象

redirect_to 重定向到指定的url

django.views.generic.date_based模块

这个模块主要处理“按时间查看存档”的功能,来源于新闻出版行业。具体包括:

archive_index 最顶级的归档,列出所有年份及指定数量的最新对象

archive_year 按年归档,列出所有拥有对象的月份

archive_month 按月归档,列出本月的所有对象,找到拥有对象的上一个、下一个月份

archive_week 按周归档,列出本周的所有对象

archive_day 按日归档,列出当天的所有对象,找到拥有对象的上一个、下一个日期

archive_today 当前日期(今天)的按日归档

object_detail 显示按照年/月/日/序号找到的对象

这些通用视图函数不再一一介绍,可以参考Django API文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: