django通用视图
通用视图
1. 前言
2. 使用通用视图
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r'^about/$', direct_to_template, {'template': 'about.html'}),
)

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from mysite.books.views import about_pages
urlpatterns = patterns('',
(r'^about/$', direct_to_template, {
'template': 'about.html'
}),
(r'^about/(\w+)/$', about_pages),
)
# view.py
from django.http import Http404
from django.template import TemplateDoesNotExist
from django.views.generic.simple import direct_to_template
#由正则匹配的参数
def about_pages(request, page):
try:
return direct_to_template(request, template="about/%s.html" % page)#返回的HttpResponse
except TemplateDoesNotExist:
raise Http404()

安全问题的题外话

3. 用于显示对象内容的通用视图
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
#urls.py
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from mysite.books.models import Publisher
publisher_info = {
'queryset': Publisher.objects.all(),
'template_name': 'publisher_list_page.html',
}
urlpatterns = patterns('',
url(r'^publishers/$', list_detail.object_list, publisher_info),
)
<h2>Publishers</h2>
<ul>
{% for publisher in object_list %}
<li>{{ publisher.name }}</li>
{% endfor %}
</ul>

4. 通用视图的几种扩展用法
4.1 自定义结果集的模板名
'queryset': Publisher.objects.all(),
'template_name': 'publisher_list_page.html',
'template_object_name': 'publisher',
}
<ul>
{% for publisher in publisher_list %}
<li>{{ publisher.name }}</li>
{% endfor %}
</ul>
4.2 增加额外的context
'queryset': Publisher.objects.all(),
'template_object_name': 'publisher',
'template_name': 'publisher_list_page.html',
'extra_context': {'book_list': Book.objects.all()}
}
<ul>
{% for publisher in publisher_list %}
<li>{{ publisher.name }}</li>
{% endfor %}
</ul>
<h2>Book</h2>
<ul>
{% for book in book_list %}
<li>{{ book.title }}</li>
{% endfor %}
</ul>

return Book.objects.all()
publisher_info = {
'queryset': Publisher.objects.all(),
'template_object_name': 'publisher',
'template_name': 'publisher_list_page.html',
'extra_context': {'book_list': get_books},
}
'queryset': Publisher.objects.all(),
'template_object_name': 'publisher',
'extra_context': {'book_list': Book.objects.all},
}
4.3 查看结果集的子集
'queryset': Book.objects.filter(publisher__name='Apress'),
'template_name': 'books/apress_list.html',
}
urlpatterns = patterns('',
url(r'^books/apress/$', list_detail.object_list, apress_books),
)
4.4 更灵活的结果集操作
urlpatterns = patterns('',
url(r'^publishers/$', list_detail.object_list, publisher_info),
url(r'^books/(\w+)/$', books_by_publisher),
)
#views.py
from django.shortcuts import get_object_or_404
from django.views.generic import list_detail
from mysite.books.models import Book, Publisher
def books_by_publisher(request, name):
# Look up the publisher (and raise a 404 if it can't be found).
publisher = get_object_or_404(Publisher, name__iexact=name)
# Use the object_list view for the heavy lifting.
return list_detail.object_list(
request,
queryset = Book.objects.filter(publisher=publisher),
template_name = 'books_by_publisher.html',
template_object_name = 'book',
extra_context = {'publisher': publisher}
)
4.5 利用通用视图做额外工作
from mysite.books.views import author_detail
urlpatterns = patterns('',
# ...
url(r'^authors/(?P<author_id>\d+)/$', author_detail),
# ...
)
#views.py
import datetime
from django.shortcuts import get_object_or_404
from django.views.generic import list_detail
from mysite.books.models import Author
def author_detail(request, author_id):
# 执行通用视图函数,但不立即返回HttpResponse对象
response = list_detail.object_list(
request,
queryset = Author.objects.all(),
object_id = author_id,
)
# 记录访问该作者的时间
now = datetime.datetime.now()
Author.objects.filter(id=author_id).update(last_accessed=now)
# 返回通用视图生成的HttpResponse对象
return response
response = list_detail.object_list(
request,
queryset = Author.objects.all(),
mimetype = 'text/plain',
template_name = 'author_list.txt'
)
#修改响应格式,使其的内容不直接显示在网页中,而是储存在文件中,下载下来。
response["Content-Disposition"] = "attachment; filename=authors.txt"
return response
<ul>
{% for author in object_list %}
<li>{{ author.first_name }}</li>
{% endfor %}
</ul>
运行结果为生成authors.txt文件并提供下载:

django通用视图的更多相关文章
- Django通用视图APIView和视图集ViewSet的介绍和使用
原 Django通用视图APIView和视图集ViewSet的介绍和使用 2018年10月21日 14:42:14 不睡觉假扮古尔丹 阅读数:630 1.APIView DRF框架的视图的基类是 ...
- Django通用视图执行过程
使用通用视图后,Django请求处理过程(以ListView为例):在我们自定义的视图中: class IndexView(ListView): template_name = 'blog/index ...
- django通用视图(类方法)
这周是我入职的第一周,入职第一天看到嘉兴大佬的项目代码.视图中有类方法,我感到很困惑. 联想到之前北京融360的电话面试,问我有无写过类方法……看来有必要了解下视图的类方法,上网搜了很多,原来这就是所 ...
- 6:django 通用视图
上一节我们介绍了django视图函数里面几个常用的函数,这节我们来看一下django为我们提供的一些通用视图吧 在最后面有我自己的示例代码,html部分太多了就不贴了 “简单”视图函数 正如名字所言, ...
- django通用视图之TemplateView和ListView简单介绍
django支持类视图,与此同时django为我们提供了许多非常好用的通用视图供我们使用,这其中TemplateView.ListView和DetailView是我们经常使用到的,这里就对Templa ...
- Django通用视图APIView和视图集ViewSet的介绍和使用(Django编程-1)
1.APIView DRF框架的视图的基类是 APIView APIView的基本使用和View类似 Django默认的View请求对象是 HttpRequest,REST framework 的请求 ...
- Django - 通用视图
urls.py from . import views ... url(r'^$', views.IndexView.as_view, name="index"), url(r'^ ...
- Django:之Sitemap站点地图、通用视图和上下文渲染器
Django中自带了sitemap框架,用来生成xml文件 Django sitemap演示: sitemap很重要,可以用来通知搜索引擎页面的地址,页面的重要性,帮助站点得到比较好的收录. 开启si ...
- Django 1.6 基于类的通用视图
Django 1.6 基于类的通用视图 最初 django 的视图都是用函数实现的,后来开发出一些通用视图函数,以取代某些常见的重复性代码.通用视图就像是一些封装好的处理器,使用它们的时候只须要给出特 ...
随机推荐
- asp.net MVC提高开发速度(创建项目模板)
- 你是否有遇到过某个实体类字段(属性)过多的情况,不想每次点的话戳进来(C# 反射)
贴上一段代码: bureaucraticEntities apply = new bureaucraticEntities(); Type tapp= app.GetType(); Type ttmp ...
- ASP.NET WebApi 路由配置【转】
一.路由介绍 ASP.NET Web API路由是整个API的入口.我们访问某个资源就是通过路由映射找到对应资源的URL.通过URL来获取资源的. 对于ASP.NET Web API内部实现来讲,我们 ...
- 实现QQ第三方登录教程(php)
参看地址:http://www.bcty365.com/content-10-2945-1.html
- c++ vs 快捷方式
强迫智能感知:Ctrl+J: 强迫智能感知显示参数信息:Ctrl-Shift-空格: Ctrl+E,D ----格式化全部代码 Ctrl+A+K+F Ctrl+E,F ----格式化选中的代码 Ctr ...
- 利用jstack 找到异常代码
1.top找出耗时pid进程或ps -ef |grep xxx 找出pid 2.ps p 3036 -L -o pcpu,pid,tid,time,tname,cmd 3036为pid 3.prin ...
- 帝国CMS7.2新增多图同时上传插件,上传多图效率更高
原来上传多图文件,需要挨个选择文件,然后再点批量上传,比较麻烦.所以帝国CMS7.2新增了多图上传插件:为采用FLASH方式实现同时选择多个图片一起上传,提高多图上传效率. 帝国CMS多图上传插件特性 ...
- [Scikit-learn] Dynamic Bayesian Network - HMM
Warning The sklearn.hmm module has now been deprecated due to it no longer matching the scope and th ...
- 第五章 面向方面编程___AOP入门
上一篇讲了 AOP 和 OOP 的区别,这一次我们开始入门 AOP .实现面向方面编程的技术,主要分为两大类: 一是 采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行: ...
- laravel windows下安装 gulp 和 laravel-elixir
1)首先,确定一下你装了nodejs和npm了没?没装的话,到官网去下载最新版,传送门:https://nodejs.org/en/ npm 不需要单独安装,安装完 nodejs 就自带 npm 的了 ...