Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26314786
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2009-08-04 17:39:29

Django内建通用视图可以实现如下功能:

  • 完成常用的简单任务:重定向到另一个页面以及渲染一个指定的模板。

  • 显示列表和某个特定对象的详细内容页面。第8章中提到的 event_listentry_list 视图就是列表视图的一个例子。一个单一的 event 页面就是我们所说的详细内容页面。

  • 呈现基于日期的数据的年/月/日归档页面,关联的详情页面,最新页面。Django Weblogs (http://www.djangoproject.com/weblog/)的年、月、日的归档就是使用通用视图 架构的,就像是典型的新闻报纸归档。

  • 允许用户在授权或者未经授权的情况下创建、修改和删除对象。

综上所述,这些视图为开发者日常开发中常见的任务提供了易用的接口。


我的心得:通用视图是为了减少重复代码的编写工作量而做的!

我们要做的就是要知道这个通用视图方法的参数是哪些。我们要传过去给它!

urlpatterns = patterns('',
    (r'^about/$',direct_to_template,{'template':'newtest/about.html'}),
)

direct_to_template这个通用视图。参数是template值。这里面我不用再自己写视图方法了实现的功能是跟这个一样的。这样就可以减少很多的代码编写量了!


通用视图的常见参数:将参数记住那使用起来会比较方便的!

Table D-1. Common Arguments to Generic Views
参数 描述
allow_empty A Boolean specifying whether to display the page if no objects are available. If this is False and no objects are available, the view will raise a 404 error instead of displaying an empty page. By default, this is False .
context_processors A list of additional template-context processors (besides the defaults) to apply to the views template. See Chapter 10 for information on template context processors.
extra_context A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.
mimetype The MIME type to use for the resulting document. It defaults to the value of the DEFAULT_MIME_TYPE setting, which is text/html if you havent changed it.
queryset A QuerySet (i.e., something like Author.objects.all() ) to read objects from. See Appendix C for more information about QuerySet objects. Most generic views require this argument.
template_loader The template loader to use when loading the template. By default, its django.template.loader . See Chapter 10 for information on template loaders.
template_name The full name of a template to use in rendering the page. This lets you override the default template name derived from the QuerySet .
template_object_name The name of the template variable to use in the template context. By default, this is 'object' . Views that list more than one object (i.e., object_list views and various objects-for-date views) will append '_list' to the value of this parameter.


简易通用视图

  django.views.generic.simple 模块包含了简单视图,用来处理一些公共的事情:在不需要视图逻辑的时候渲染一个模板和发出一个重定向。

(只是简单的渲染下常用的公共模板或发出一个重定向操作就行了!并不处理一些常用视图逻辑!)


渲染模板

视图函数 : django.views.generic.simple.direct_to_template

该视图渲染一给定模板,给其传递一个 {{ params }} 的模板变量,该变量是在URL中被获取的一个自典型参数

示例:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)

请求 /foo/ 时就会渲染模板 foo_index.html ,而请求 /foo/15/ 就会附带一个值为 15 的context来渲染模板 foo_detail.html

必要参数

  • template :模板的全名。

通用视图方法接收到一个常用参数进来!即模板全名!
要渲染的模板名称!
我用通用视图直接使用不必要再自己手工去写视图层代码的!非常方便的!



重定向到另外一个URL

视图函数django.views.generic.simple.redirect_to

这个视图将源URL重定向到目的URL,目的URL中可以带有字典风格的格式化字符串,在源URL中被捕获的参数可以被格式化输出到目的URL中。

如果目的URL是 None ,Django会返回HTTP 410(文件丢失)错误。

必要参数

  • url :被重定向到的URL,它是个字符串。如果是 None 的话,就返回410(文件丢失)错误。


列表/详细 通用视图(处理比如说新闻列表页、详细页的常用视图)

列表/详细 通用视图(位于模块 django.views.generic.list_detail 中)处理了一种常见的应用,它在一个视图中显示一个项的列表,在另外一个视图中显示列表中某个具体项的细节。

视图函数 : django.views.generic.list_detail.object_list

使用这个视图来显示一个对象列表页面。

from mysite.books.models

import Author from django.conf.urls.defaults

import * from django.views.generic

import list_detail

author_list_info = { 'queryset' : Author.objects.all(),

'allow_empty': True, }

urlpatterns = patterns('', (r'authors/$',

list_detail.object_list, author_list_info) )

这个通用视图的参数是author_list_info

是一个字典类型的数据值!


必要参数

  • queryset : 要列出的对象的 QuerySet (参见表 D-1).(显示出来的结果集)

可选参数

  • paginate_by : 一个整形数,用来制定每页显示多少条记录。如果指定了这个参数,那么视图将会把记录按照paginate_by的值来安排每页记录的数量。视图将会通过一个page的查询字符串参数传入一个以0开始的页号(通过GET来获取page的值),或者在配置url的时候指定一个page变量。详细请查看Pagination章节的说明。(分页的参数值)

Additionally, this view may take any of these common arguments described in Table D-1:

  • allow_empty

  • context_processors

  • extra_context

  • mimetype

  • template_loader

  • template_name

  • template_object_name


模板名称

If template_name isnt specified, this view will use the template /_list.html by default。

如果上面的可选参数中的模板名称没有指定的话就使用默认的!

/_list.html

当然我们可以指定哦!

If the results are paginated, the context will contain these extra variables:(如果有分页存在的话就要考虑如下的可选参数)

  • results_per_page : The number of objects per page. (This is the same as the paginate_by parameter.)(每页显示条数)

  • has_next : 一个布尔值表示是否有下一页.

  • has_previous : 一个布尔值表示是否有上一页.

  • page : 表示当前页的页码,是一个整数(如第9页),第一页是从1开始计算的。

  • next : The next page number, as an integer. If theres no next page, this will still be an integer representing the theoretical next-page number. This is 1-based.

  • previous : The previous page number, as an integer. This is 1-based.

  • pages : The total number of pages, as an integer.

  • hits : The total number of objects across all pages, not just this page.


(r'^objects/page(?P[0-9]+)/$', 'object_list', dict(info_dict))

Pass the page number via the page query-string parameter. For example, a URL would look like this:

/objects/?page=3

其分页的方法就这么简单的!

Detail Views

View function : django.views.generic.list_detail.object_detail

详细的信息页面。不是列表页面了哦!区别开来的!


from mysite.books.models import Author from django.conf.urls.defaults import * from django.views.generic import list_detail author_list_info = { 'queryset' : Author.objects.all(), 'allow_empty': True, } **author_detail_info = {** **"queryset" : Author.objects.all(),** **"template_object_name" : "author",** **}** urlpatterns = patterns('', (r'authors/$', list_detail.object_list, author_list_info), **(r'^authors/(?Pd+)/$', list_detail.object_detail, author_detail_info),** )

就是说:当选择了某一个内容标题之后就会进入到详细的说明信息页面出来!

必要参数

  • queryset : A QuerySet that will be searched for the object (see Table D-1).

and either (必选参数值)

  • object_id : The value of the primary-key field for the object.

或者

  • slug : The slug of the given object. If you pass this field, then the slug_field argument (see the following section) is also required.

This view may also take these common arguments (see Table D-1):

可选参数:

  • context_processors

  • extra_context

  • mimetype

  • template_loader

  • template_name

  • template_object_name


模板名

If template_name and template_name_field arent specified, this view will use the template /_detail.html by default.



Create/Update/Delete Generic Views

django.views.generic.create_update 视图包括了创建、编辑和删除对象所需的处理函数。(能够实现数据的增、删、修改操作的通用视图方法)


View function : django.views.generic.create_update.create_object 添加新记录的通用视图方法!

Optional Arguments

post_save_redirect : A URL to which the view will redirect after saving the object. By default, its object.get_absolute_url() .

《在保存之后会跳转到哪个URL地址去!》

post_save_redirect : May contain dictionary string formatting, which will be interpolated against the objects field attributes. For example, you could use post_save_redirect="/polls/%(slug)s/" .


login_required : A Boolean that designates whether a user must be logged in, in order to see the page and save changes. This hooks into the Django authentication system. By default, this is False .

If this is True , and a non-logged-in user attempts to visit this page or save the form, Django will redirect the request to /accounts/login/ .

This view may also take these common arguments (see Table D-1):

  • context_processors

  • extra_context

  • template_loader

  • template_name



If template_name isnt specified, this view will use the template /_form.html by default.  如果没有指定这个参数值就会有一个默认值的!



Update Object View

Update Object View

View function : django.views.generic.create_update.update_object







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