django搭建web (五) views.py
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文件中渲染,显示
django搭建web (五) views.py的更多相关文章
- django搭建web (二) urls.py
URL模式: 在app下的urls.py中 urlpatterns=[ url(正则表达式,view函数,参数,别名,前缀)] urlpatterns=[ url(r'^hello/$',hello. ...
- django搭建web (四) models.py
demo 该demo模型主要是用于问题,选择单个或多个答案的问卷形式应用 # -*- coding: utf-8 -*- from __future__ import unicode_literals ...
- django搭建web (三) admin.py -- 待续
demo 关于模型myQuestion,myAnswer将在后述博客提及 # -*- coding: utf-8 -*- from __future__ import unicode_literals ...
- PyCharm社区版+Django搭建web开发环境-2
接上一篇:PyCharm社区版+Django搭建web开发环境-1 1. 创建好django项目并建立app应用:web 2. setting.py:配置app应用 INSTALLED_APPS = ...
- python +Django 搭建web开发环境初步,显示当前时间
1.python 的安装 网上很多关于django跟python 开发的资料,这块我正在实习准备用这个两个合起来搞一个基于web 的东西出来现在开始学习,写点东西记录一下心得. 开发环境是window ...
- Python & PyCharm & Django 搭建web开发环境
一.安装软件 1.安装 Python 2.7.PyCharm.pip(Python包管理工具).Django ( pip install Django) 二.部署 1.PyCharm 新建Django ...
- django搭建web (一)
建立工程 django-admin.py startproject project_name 建立app python manage.py startapp app_name 将app添加进工程中 在 ...
- PyCharm社区版+Django搭建web开发环境-1
PyCharm开源社区版不像商业版那样可以直接通过Django来创建项目,必须通过以下几个步骤进行: 1. 创建项目:在cmd命令行下输入:django-admin startproject Demo ...
- PyCharm社区版+Django搭建web开发环境
PyCharm开源社区版不像商业版那样可以直接通过Django来创建项目,必须通过以下几个步骤进行: 1. 创建项目:在cmd命令行下输入:django-admin startproject Demo ...
随机推荐
- js常见排序
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 描述下@Component,@Repository,@Service,@Scope,@Autowired,@Inject,@Value标记的作用
1.@Component为通用注解. 2.@Repository为持久层组件注解. 3.@Service为业务层组件注解. 4.@Scope为Bean的作用域注解. 5.@Autowired,@Inj ...
- Spring Boot Security 基于角色的访问控制
@Override protected void configure(HttpSecurity http) throws Exception { //如果配置为需要登录 if (needLogin) ...
- 【MyBatis源码分析】Configuration加载(上篇)
config.xml解析为org.w3c.dom.Document 本文首先来简单看一下MyBatis中将config.xml解析为org.w3c.dom.Document的流程,代码为上文的这部分: ...
- Facebook兆级别图片存储及每秒百万级别图片查询原理
前言 Facebook(后面简称fb)是世界最大的社交平台,需要存储的数据时刻都在不断剧增(占比最大为图片,每天存储约20亿张,大概是微信的三倍). 那么问题来了,fb是如何存储兆级别的图片?并且又是 ...
- nginx location匹配顺序及CI框架的nginx配置
Nginx location匹配顺序如下: 用前缀字符串定义的location规则对URI进行匹配测试. =号定义了精确的前缀字符串匹配,如果发现精确匹配则使用当前规则.否则继续下一步匹配. 匹配其它 ...
- python web开发-flask中消息闪现flash的应用
Flash中的消息闪现,在官方的解释是用来给用户做出反馈.不过实际上这个功能只是一个记录消息的方法,在某一个请求中记录消息,在下一个请求中获取消息,然后做相应的处理,也就是说flask只存在于两个相邻 ...
- TPYBoard v102 DIY照相机(视频和制作流程)
前段时间的帖子,利用TPYBoard v102做的DIY照相机,周末实物终于做出来了,加了两个按键模块和一个5110,做的有点糙啊----望大家勿怪,哈哈哈.拍出来图片还算清晰,串口摄像头模块用的30 ...
- present(模态)实现出push的效果
在present加上这个转场动画,取消掉原来的转场动画 CATransition *animation = [CATransitionanimation]; animation.durati ...
- Win调整和小技巧
推荐win下一些个人爱用的工具软件(以及使用心得)和一些系统调整方法,让win下不尽人意的设置发生小小变化,让整天摸着电脑的ITer们的生活更有乐趣. 本人酷爱收集一些好用的软件,若各位也对某个或某些 ...