django_视图相关
使用通用视图(返回静态页面)
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template urlpatterns = patterns('',(r'^about/$', direct_to_template, {'template': 'about.html'})
无法捕捉没有该模板的错误,支持重写
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)
except TemplateDoesNotExist:
raise Http404()
对象的通用视图
帮助你很容易 地生成对象的列表和明细视图,当然还需要编写一个模板。 我们可以通过在额外参数字典中包含一个template_name键来显式地告诉object_list视图使用哪个模板
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('',
(r'^publishers/$', list_detail.object_list, publisher_info)
)
更改变量名
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from mysite.books.models import Publisher # 添加额外的Context
# 在模板中,通用视图会通过在template_object_name后追加一个_list的方式来创建一个表示列表项目的变量名。 publisher_info = {
'queryset': Publisher.objects.all(),
'template_object_name': 'publisher',
'extra_context': {'book_list': Book.objects.all}
} urlpatterns=patterns('',(r'^publishers/$',list_detail.object_list,publisher_info))
用函数包装来处理复杂的数据过滤
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/books_by_publisher.html',
template_object_name = 'book',
extra_context = {'publisher': publisher}
)
django_视图相关的更多相关文章
- 从零开始实现ASP.NET Core MVC的插件式开发(八) - Razor视图相关问题及解决方案
标题:从零开始实现ASP.NET Core MVC的插件式开发(八) - Razor视图相关问题及解决方案 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun ...
- Django框架03 /视图相关
Django框架03 /视图相关 目录 Django框架03 /视图相关 1. 请求相关 2.响应相关 3.FBV和CBV 视图(视图函数和视图类) 3.1 类视图 CBV 3.2 视图函数 FBV ...
- iOS,视图相关
1.移除视图的所以子视图 2.自定义视图(UIView) 3.处理悬浮窗口(类似微信视频),等比缩放 4.自定义前面视图(可以手写字) 5.图片拉伸的几种方式,计算文本占用空间大小 6.UILable ...
- sql server系统表和视图相关的语句
一.系统表 数据字典的详细信息请查SQL SERVER BOL,这里仅列出一部分. 1.1.sysservers 1.查看所有本地服务器及链接服务器 select * from master..sys ...
- DM7的闪回功能及动态新能视图相关SQL总结
DM7的闪回功能默认是关闭的,需要在dm.ini中设置参数: ENABLE_FLASHBACK = 1 UNDO_RETENTION = 900 意思为可以进行900s以内的闪回查询.下面是使用该功能 ...
- day73:drf:drf视图相关类&路由Routers&创建虚拟环境
目录 1.APIView 2.GenericAPIView:通用视图类 3.5个视图扩展类:ListModelMixin,CreateModelMixin,RetrieveModelMixin,Upd ...
- Asp.net MVC 4 视图相关和其他
@{ Layout = “…”} To define layout page Equivalent to asp.net master-page 要定义相当于ASP.NET母版页的页面布局 @mode ...
- iOS开发中视图相关的小笔记:push、modal、popover、replace、custom
在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的. 在iphone中,segue有:push,modal,和custom三种不同的类型, ...
- sqlserver数据库 视图相关
1.首先创建一个视图 方法一:右键解决 方法二:脚本 create view view_test AS select * from t1 GO 2.删除视图 方法1:右键解决 方法2:脚本 if ex ...
随机推荐
- Ubuntu系统下安装完成tomcat进入管理页面
首先先启动tomcat cd /usr/local/tomcat8. ./bin/startup.sh 然后再打开浏览器 在地址栏中输入 http:/localhost:
- python 3 安装
如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!!!! 不要动现有的python2环境! 一.安装p ...
- 20165223《网络对抗技术》Exp 9 Web安全基础
目录 -- Web安全基础 ★ 实验说明 实验目标 基础问答 实验准备 ★ 实验内容 SQL注入攻击 1. 命令注入(Command Injection) 2. 数字型注入(Numeric SQL I ...
- jQuery Ajax calls and the Html.AntiForgeryToken()
jQuery Ajax calls and the Html.AntiForgeryToken() https://stackoverflow.com/a/4074289/3782855 I use ...
- laravel 查询数据库first()返回的数据转数组
使用 get_object_vars()可以将他抓转为数组get_object_vars — 返回由对象属性组成的关联数组: 在laravel中其实还可以用 toArray(); json_decod ...
- Django 测试开发4 Django 模板和分页器
Django结合前端框架Bootstrap来开发web页面.pip install django-bootstrap3 在setting.py添加‘bootstrap3’. 继承模板. 在base页面 ...
- world: 对比两个文档
1. 2. 3. 4.
- Android插件化(五):OpenAtlasの四大组件的Hack
Android插件化(五):OpenAtlasの四大组件的Hack 转 https://www.300168.com/yidong/show-2776.html 核心提示:引言到目前为止,我们已经 ...
- Windows10下Anaconda虚拟环境下安装pycocotools
0 - 步骤 安装visualcppbuildtools_full.exe(链接:https://blog.csdn.net/u012247418/article/details/82314129) ...
- hadoop格式化
1.hadoop启动时,namenode没有启动起来 1.删除 namenode产生的临时文件 tmp 2.删除datanode的数据,否则也不行.(如果datanode有数据,请自己备份) ./bi ...