python烧瓶 ​​- 提供静态文件 [英] python flask - serving static files

查看:118
本文介绍了python烧瓶 ​​- 提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用烧瓶来提供静态文件。我不知道如何使用url_for函数。所有我的路线生成动态内容工作正常,我已经导入url_for,但是当我有这个代码:

  @app。 route('/')
def home():
return url_for('static',filename ='hi.html')

与我坐在静态目录中的'hi.html'文件(里面有一些基本的html)一起,当我加载页面的时候,字面上是这样的: p>


/static/hi.html


我只是使用url_for不正确?

解决方案

url_for 该文件的URL。这听起来像你想重定向到该文件的URL。相反,您只是将URL的文本作为响应发送给客户端。

  from flask import url_for,redirect 

@ app.route('/')
def home():
return redirect(url_for('static',filename ='hi.html'))


I'm trying to serve a static file using flask. I don't know how to use the url_for function. All my routes generating dynamic content are working fine, I've imported url_for, but when I have this code:

@app.route('/')
def home():
    return url_for('static', filename='hi.html')

Along with my 'hi.html' file (which has some basic html in it) sitting in directory static, what I get when I load the page is literally this:

/static/hi.html

Am I just using url_for incorrectly?

解决方案

url_for just returns, precisely, the URL for that file. It sounds like you want to redirect to the URL for that file. Instead, you are just sending the text of the URL to the client as a response.

from flask import url_for, redirect

@app.route('/')
def home():
    return redirect(url_for('static', filename='hi.html'))

这篇关于python烧瓶 ​​- 提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆