96 - 用Flask实现转发与重定向

举报
ruochen 发表于 2021/03/28 23:54:09 2021/03/28
【摘要】 1. 请解释什么是转发与重定向,它们有什么区别? 转发: 对用户是透明的,或者在Web浏览器中的Url是不会变的,在服务端会根据请求Url去读取特定的资源,并将资源的内容返回给客户端服务端资源对于用户不一定是可访问的http://localhost:5000/test.html 重定向 用户是可见的,Web浏览器地址栏中的Url将改变服务端资源必须是可访问的http:...

1. 请解释什么是转发与重定向,它们有什么区别?

  • 转发:
    • 对用户是透明的,或者在Web浏览器中的Url是不会变的,在服务端会根据请求Url去读取特定的资源,并将资源的内容返回给客户端
    • 服务端资源对于用户不一定是可访问的
    • http://localhost:5000/test.html
  • 重定向
    • 用户是可见的,Web浏览器地址栏中的Url将改变
    • 服务端资源必须是可访问的
    • http://localhost:5000/test.html
    • http://localhost:5000/abc.html

2. 在Falsk中如何转发和重定向资源

test1.txt

Hello world!

  
 
  • 1
from flask import *
app = Flask(__name__)

# 转发
@app.route('/test')
def test(): return app.send_static_file('test1.txt') # 重定向
@app.route('/abc')
def abc(): return redirect('/static/test1.txt')

if __name__ == '__main__': app.run()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
 * Serving Flask app "__main__" (lazy loading)
 * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [31/Mar/2020 16:09:03] "GET /test HTTP/1.1" 404 -

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

97 - Flask中如何在Jinjia2模块中使用Python列表

文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。

原文链接:ruochen.blog.csdn.net/article/details/105264305

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200