一、Python Django 之 Views 数据交互

http请求中产生两个人核心对象:

http请求:HttpRequest对象

http响应:HttpReponse对象

所在位置django.http

之前我们用到的参数request就是HttpRequest 检测方法

二、HttpRequest对象

1、

2、

3、

三、HttpReponse对象

1、HttpReponse返回数据

return HttpResponse("<h1>ok</h1>")

2、render渲染

return render(req,"index.html",{"user_list":user_list})

注意:render渲染比HttpResponse复杂,完成同样的工作render只需要一步,HttpResponse却需要

Trmplate与Context等步骤。

3、render_to_response渲染

1)views

n="hope"

return render_to_response("index.html",{"name":n})

2)templates

<html>

<head>

</head>

<body>

<h1>{{ name }}</h1>

</body>

</html>

1)好处

使用render_to_response比render简便,省去了req参数

n="hope"

return render_to_response("index.html",{"name":n})

2)坏处

建议使用return,不用render_to_response

4、redirect重定向

return redirect(“www.baidu.com”)

注意:

--1 url: 原本urlpatterns按照模板使用的{},但是在return redirect("/home/")时,总是报错 'set' object is not reversible,

后来,将{}的集合写法换成[]列表写法则不再报错,return redirect("/home/")生效。

1)url

urlpatterns = [
path('admin/', admin.site.urls),
path('cur_time/', views.cur_time),
path('hello/',views.hello),
path('login/', views.login),
path('home/', views.home)] # urlpatterns = {
# path('admin/', admin.site.urls),
# path('cur_time/', views.cur_time),
# # path('userInfo/',views.userInfo),
# # path('login/', views.login),
# # path('home/', views.home),
# path('hello/',views.hello),
# }

2)views

from django.shortcuts import render,HttpResponse,render_to_response,redirect
import datetime
from blog import models def login(req):
if req.method == "POST":
if 1:
return redirect("/home/")
return render(req,"login.html") def home(req):
name="xihaohu"
return render(req,"home.html",{"name":name})

3)templates

--1 login

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="/login/" method="post">
<input type="text" name="username">
<input type="text" name="pwd">
<input type="submit" name="submit">
</form>
</body>
</html>

--2 home

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>welcom {{ name }}</h1>
</body>
</html>

Python Django 之 Views HttpRequest HttpReponse的更多相关文章

  1. python :Django url /views /Template 文件介绍

    1,Django URL 路由系统 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django ...

  2. Django之views系统

    Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...

  3. [python] python django web 开发 —— 15分钟送到会用(只能送你到这了)

    1.安装python环境 1.1 安装python包管理器: wget https://bootstrap.pypa.io/get-pip.py sudo python get-pip.py   1. ...

  4. Python Django(WEB电商项目构建)

    (坚持每一天,就是成功) Python Django Web框架,Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型M,模板T和视图V组成. 安装Pyth ...

  5. 七、Django之Views

    一.概述 视图就是python中的函数,我们通常也称为:视图函数. 视图一般被定义在“app/views.py”中. 视图负责接受Web请求(HttpRequest)URL,进行逻辑处理,并返回Web ...

  6. Django之views.py视图函数学习

    视图函数: 视图函数时存在django项目中的应用程的一个名为views.py的文件模块: 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 一 ...

  7. python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器

    python3.5 manage.py runserver python Django教程 之模板渲染.循环.条件判断.常用的标签.过滤器 一.Django模板渲染模板 1. 创建一个 zqxt_tm ...

  8. python Django教程 之 安装、基本命令、视图与网站

    python  Django教程  之 安装.基本命令.视图与网站 一.简介 Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 w ...

  9. python django 多级业务树形结构规划及页面渲染

    概述: 在项目中,父级到子级结构并不少见,如果仅仅的两层树形结构,我们可以使用数据库的外键设计轻松做到,子级业务表设计一字段外键到父级业务表,这样子到父.父到子的查询都非常简单. 但是往往父子结构会有 ...

随机推荐

  1. luogu 1004 方格取数

    题目描述 设有 $N \times N$ 的方格图 $(N \le 9)$ ,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字 $0$ .如下图所示(见样例): A 0 0 0 0 0 0 ...

  2. POJ 1270 Following Orders(拓扑排序)题解

    Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...

  3. 【分词器及自定义】Elasticsearch中文分词器及自定义分词器

    中文分词器 在lunix下执行下列命令,可以看到本来应该按照中文”北京大学”来查询结果es将其分拆为”北”,”京”,”大”,”学”四个汉字,这显然不符合我的预期.这是因为Es默认的是英文分词器我需要为 ...

  4. UVa 208 消防车(dfs+剪枝)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. 分析 HTML 代码并提取数据

    在前面的内容中,我们已经学习了 HTML.CSS 和 XPath 的基础知识.从真实世界的网页中获取数据,关键在于如何编写合适的 CSS 或者 XPath 选择器.本节介绍一些确定选择器的简单方法.假 ...

  6. 【Golang】 可以自动生成测试用例的库--gotests

    简介 gotests是一个Golang命令行工具,它可以使编写Go的测试代码变得容易.它能基于目标源文件的函数和方法生成数据驱动测试用例,并且在此过程会自动导入任何依赖. 下面是gotests在使用S ...

  7. 【Golang 接口自动化01】使用标准库net/http发送Get请求

    发送Get请求 使用Golang发送get请求很容易,我们还是使用http://httpbin.org作为服务端来进行演示. package main import ( "bytes&quo ...

  8. Codeforces D - High Load

    D - High Load 因为要出口节点距离最小,所以除了根节点(根节点连接k个儿子)其他节点的儿子只能有一个,其他情况下的距离都比这个长,因为如果不是这样,那么根节点连接的子树数量就小与k,那么每 ...

  9. C# 中的浅表副本与深表副本

    public class Student { public int age; public Student(int age) { this.age = age; } } public class Gr ...

  10. Python mysql-常用对象

    2017-09-08 13:14:14 db = pymysql.connect(host,user,passwaord,db,chartset),charset=utf8,可以避免中文的乱码 con ...