http请求: HttpRequest

http响应: HttpResponse

所在位置:django.http

isinstance(request,HttpResponse) True->request的类型就是HttpRequest

HttpRequest对象属性:

Attribute Description
path 请求页面的全路径
method ```请求中使用的HTTP方法的字符串表示。全大写表示。例如

       if quest.method == 'GET':

do_something()

elif request.method == 'POST': 

do_something_else() ```

|GET |包含所有HTTP GET参数的类字典对象 |

|POST |包含所有HTTP POST参数的类字典对象 |

    localhost:8000/hello/?name=12345

参数名为name,在接收的时候可以如下

    def hello(request):
print(quest.GET.get('name'))
get() 如果key对应多个value,get()返回最后一个value

form-表单

response = HttpResponse("Here is the text of the web page",mimetype="text/plain")
return response
#等价
return render(request,'table.html',{'foo':'bar'}) t = loader.get_template('table.html')
c = {'foo':'bar'}
return HttpResponse(t.render(c,request),content_type="text/html") from django.shortcuts import render_to_response
return render_to_response('table.html',{'foo':'bar'})

redirect 页面跳转

from django.shortcuts import redirect
... ...
return redirect('http://www.baidu.com')

locals()

#locals()将当前作用于的变量全部传递给模板变量
return render_to response('table.html',locals())

demo

# -*- coding: utf-8 -*-
from __future__ import unicode_literals from django.shortcuts import render
from .models import myQuestion,myAnswer
# Create your views here. def research(request):
question_list = myQuestion.objects.all()
answer_list = myAnswer.objects.all()
context = {'question_list':question_list,'answer_list':answer_list}
return render(request,"polls/index.html",context)

在该demo中

首先从models中引入模型myQuestion,myAnswer

from .models import myQuestion,myAnswer

其次定义了一个函数hello,这个地方的hello名称要与urls.py中名称对应,例如hello出现在urls.py如下:

urlpatterns = [
url(r'^$',views.hello,name = 'hello'),
]

之后调用

question_list = myQuestion.objects.all()

函数,将myQuestion模型的全部变量以列表形式赋值给question_list,下句类似

然后

context = {'question_list':question_list,'answer_list':answer_list}

将列表转成字典,并起上名称,一同赋值给context

最后

return render(request,"polls/index.html",context)

将内容context放进templates/polls/index.html文件中渲染,显示

最新文章

  1. [canvas]利用canvas绘制自适应的折线图
  2. 开源项目asmjit——调用自定义方法demo以及windbg调试
  3. Newtonsoft.Json 通过 JObject 读取 json对像 超简单
  4. 1-File类的使用
  5. uniq命令注意事项,检查重复行的时候,只会检查相邻的行。
  6. dpi,ppi,dip,dp,px和sp
  7. git 使用钩子直接推送到工作目录
  8. 【Qt】Qt之自定义界面(实现无边框、可移动)【转】
  9. Intent是什么?
  10. Facebook 正式开源其大数据查询引擎 Presto
  11. ElasticSearch大批量数据入库
  12. WCF服务最近经常死掉
  13. 基于TcpListener实现最简单的http服务器
  14. 解决Mybatis连接Sql server 出现 Cannot load JDBC driver class 'com.mysql.jdbc.Driver '的问题
  15. struts2 之 ServletAPI
  16. 关于dedecms的操作
  17. CSS选择器之通配符选择器和多元素选择器
  18. Caused by:java.sql.SQLException:ORA-01008:并非所有变量都已绑定
  19. 排列熵算法简介及c#实现
  20. 轻松搞定RocketMQ入门

热门文章

  1. JSP页面输出九九乘法表--JSP基础
  2. Spring AOP介绍
  3. java编程思想第四版第九章总结
  4. 【BZOJ3238】差异(后缀自动机)
  5. [Luogu4074][WC2013]糖果公园
  6. datatable 多字段 排序;
  7. 20.1章JSON语法
  8. 互联网产品mysql数据库设计总结
  9. 关于Android sdkmanager目录结构的总结
  10. mysql主从复制的基本原理