一、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. bzoj 3343: 教主的魔法

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 924  Solved: 402[Submit][Status][Discuss] Descriptio ...

  2. Delphi XE5 for Android (七)

    Delphi XE5下,TMainMenu和TPopupMenu不可用,那么如何支持Android的菜单呢? 查看了一些资料,不得要领,只是摸索着先模拟一下吧. 首先在窗体上放置一个TPanel,在其 ...

  3. compile vi from source code

    1.install ncurses library (vi  depend on ncurses library) ./configure --prefix=/usr --with-termlib m ...

  4. ubuntu下安装mkfs.jffs工具

    一.环境 Os: ubuntu 16.04 二.安装 2.1安装依赖库 sudo apt install zlib1g-dev liblzo2-dev uuid-dev 2.2编译安装mtd-util ...

  5. 括号序和dfs序

    记得清北讲过括号序和dfs序,忘记了 dfs序 dfs序就是dfs的顺序,这个好记 就是在dfs遍历树的时候,将每个结点开始时记录一次,结束时记录一次 而且一个子树可以表示为连续的一段, 只有子树操作 ...

  6. LOJ #10222. 「一本通 6.5 例 4」佳佳的 Fibonacci

    题目链接 题目大意 $$F[i]=F[i-1]+F[i-2]\ (\ F[1]=1\ ,\ F[2]=1\ )$$ $$T[i]=F[1]+2F[2]+3F[3]+...+nF[n]$$ 求$T[n] ...

  7. 如何解决Visual Studio2010 编译时提示系统找不到指定文件问题

    前一段时间,开始使用vs2010编写程序,可是在编译的时候总是报错,提示系统找不到指定文件,导致无法正常运行程序,花了好久时间终于找到原因解决,是因为常规的输出目录 要与链接的常规的输出文件要相对应. ...

  8. 《剑指offer》第二十七题(二叉树的镜像)

    // 面试题27:二叉树的镜像 // 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像. #include <iostream> #include "BinaryTree ...

  9. angular5 路由变化监听

    1.路由监听 //监听路由变化this.router.events .filter(event => event instanceof NavigationEnd) .map(() => ...

  10. oracle 临时表的使用

    在oracle中,临时表分为会话级别(session)和事务级别(transaction)两种. 会话级的临时表在整个会话期间都存在,直到会话结束:事务级别的临时表数据在transaction结束后消 ...