Django shortcut functions
- django.shortcuts package提供提供帮助类和函数可以更便捷的操作MVC中的每一部分,包含:
- render(request, template_name,[dictionary],[context_instance],[content_type],[status],[current_app],[dirs]):使用给定的context填充模板,返回HttpResponse对象,该对象的content就是模板替换后的内容.render()函数同以context_instance(RequestContext)为参调用render_to_response功能相同
render#如下代码功能相同
from django.shortcuts import render def my_view(request):
# View code here...
return render(request, 'myapp/index.html', {"foo": "bar"},
content_type="application/xhtml+xml") from django.http import HttpResponse
from django.template import RequestContext, loader def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = RequestContext(request, {'foo': 'bar'})
return HttpResponse(t.render(c),
content_type="application/xhtml+xml") - render_to_response(template_name[, dictionary][, context_instance][, content_type][, dirs]):使用给定的context填充template,并返回HttpResponse object,
render_to_responsereturn render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
#如下代码功能相同 from django.shortcuts import render_to_response def my_view(request):
# View code here...
return render_to_response('myapp/index.html', {"foo": "bar"},
content_type="application/xhtml+xml") from django.http import HttpResponse
from django.template import Context, loader def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = Context({'foo': 'bar'})
return HttpResponse(t.render(c),
content_type="application/xhtml+xml") - redirect(to, [permanent=False],*args, **kwargs):返回to对象的url对应的HttpResponseRedirect,默认是临时重定向,如果permanent=true则永久重定向,参数to可以是:
- a model:这个model的get_absolute_url()函数会被调用
- a view name:urlresolvers.reverse会被调用逆向解析view name得到url
- 一个绝对或者相对url
- get_object_or_404(klass, *args, **kwargs):使用给定的model manager调用get()函数,如果没得到就抛出Http404异常
- get_list_or_404(klass, *args, **kwargs):使用给定的model manager调用filter函数返回结果转换成list,如果list为空则抛出Http404异常
- render(request, template_name,[dictionary],[context_instance],[content_type],[status],[current_app],[dirs]):使用给定的context填充模板,返回HttpResponse对象,该对象的content就是模板替换后的内容.render()函数同以context_instance(RequestContext)为参调用render_to_response功能相同
Django shortcut functions的更多相关文章
- Request对象和Response对象 JsonResponse对象 和 Django shortcut functions 和QueryDict对象
request request属性 属性: django将请求报文中的请求行.头部信息.内容主体封装成 HttpRequest 类中的属性. 除了特殊说明的之外,其他均为只读的. ''' 0.Http ...
- django shortcut function
render() render(request, template_name, context=None, content_type=None, status=None, using=None) 必须 ...
- Django之views系统
Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...
- Django视图
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- Python学习(三十二)—— Django之视图系统
转载自:http://www.cnblogs.com/liwenzhou/articles/8305104.html Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Pyth ...
- Django之视图
Django之视图 Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个 ...
- Django中的视图(view)
视图 1.什么是视图 视图就是Django项目下的view.py文件,它的内部是一系列的函数或者是类,用来专门处理客户端访问请求后处理请求并且返回相应的数据,相当于一个中央情报处理系统 2.具体视图实 ...
- Django的View(视图)
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- Django框架详细介绍---视图系统
Django视图系统 1.什么是视图 在Django中,一个视图函数/类,称为视图.实质就是一个用户自定义的简单函数,用来接收WEB请求并xing响应请求,响应的内容可以是一个HTML文件.重定向.一 ...
随机推荐
- Neo4j下执行cypher-shell时,Connection refused问题解决?
不多说,直接上干货! 问题现象 root@zhouls-/bin# ls cypher-shell neo4j neo4j-admin neo4j-import neo4j-shell tools ...
- Oracle数据库基础(二)
1.表名命名规则:必须以字母开头,不能超过30个字符,不要有Oracle保留字 2.数据类型 字符型: char :2000个字符 定长 效率高 ...
- Android 强制软键盘关闭
在Android开发过程中,有时候我们会有强制关闭软键盘的需求.比如说:现在有一个文本编辑框(testEt)和一个按钮(testBtn),我们现在点击文本编辑框testEd,这时会弹出软键盘,然后我们 ...
- IPv6地址分配
- CF960F Pathwalks_权值线段树_LIS
很不错的一道思维题. Code: #include<cstdio> #include<algorithm> #include<iostream> using nam ...
- create raid5
# umout 所有数据disk for i in {1..11};do umount /disk$i;done # 修改/etc/fstab,注释掉 /dev/sd[b-l] vim /etc/fs ...
- vertical-align到底是个啥
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align http://phrogz.net/css/vertical-align ...
- Linux 网络属性管理
Linux网络基础管理-1:IPv4 地址分类: 点分十进制:0.0.0.0-255.255.255.255 A类: 0 0000000 - 0 1111111: 1-127 网络数:126, 1 ...
- thinkphp 同一字段不同查询条件实现
搞定了 另外区间查询还可以支持更多的条件,只要是针对一个字段的条件都可以写到一起,例如:$map['name'] = array(array('like','%a%'), array('like',' ...
- Spring Security中的MD5盐值加密
在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...