分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

Flask中的 Render ?Redirect HttpResponse

发布时间:2023-09-06 02:30责任编辑:沈小雨关键词:暂无标签

1.Flask中的HTTPResponse

from flask import Flaskapp = Flask(__name__)@app.route("/") # app中的route装饰器def index(): ???# 视图函数 ???return "Hello DSB" ?# HTTPResponseapp.run(debug=True)

在Flask中的HttpResponse在我们看来其实就是直接返回字符串

2.Flask中的Redirect

from flask import Flask, redirect # 导入flask中的redirectapp = Flask(__name__)@app.route("/") # app中的route装饰器def index(): ???# 视图函数 ???return "Hello DSB" ?# HTTPResponse@app.route("/redi") # app中的route装饰器,用来指定视图函数URL地址def redi(): # 视图函数 ???return redirect("/") ???# redirect跳转至"/"

每当访问"/redi"这个地址的时候,视图函数redi会触发redirect("/") 跳转到url地址:  "/" 并会触发"/"对应的视图函数index()

3.Flask中的render(render_template)

from flask import Flask, redirect, render_templateapp = Flask(__name__)@app.route("/") # app中的route装饰器def index(): ???# 视图函数 ???return "Hello DSB" ?# HTTPResponse@app.route("/redi") # app中的route装饰器,用来指定视图函数URL地址def redi(): # 视图函数 ???return redirect("/") ???# redirect跳转至"/"@app.route("/home")  # app中的route装饰器,用来指定视图函数的url地址def home():  # 视图函数 ???return render_template("home.html")  # 渲染HTML模板返回HTML页面app.run(debug=True)

HTML模板渲染是每个Web框架中都必须有的,至于render_template的具体用法,留个悬念,往后看

注意: 如果要使用 render_template 返回渲染的模板,请在项目的主目录中加入一个目录 templates

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>Title</title></head><body><h1>我是HOME</h1></body></html>

否则可能会有一个Jinja2的异常哦

遇到上述的问题,基本上就是你的template的路径问题

Flask中的 Render ?Redirect HttpResponse

原文地址:https://www.cnblogs.com/rixian/p/10269147.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved