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

通用视图的作用

视图提供了容易的接口来处理开发人员遇到的最常见的任务
所有的这些视图被用来在你的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文档
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值