Flask 中的静态文件——robot.txt、sitemap.xml (mod_wsgi) [英] Static files in Flask - robot.txt, sitemap.xml (mod_wsgi)

查看:24
本文介绍了Flask 中的静态文件——robot.txt、sitemap.xml (mod_wsgi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么聪明的办法可以把静态文件存放在Flask的应用根目录下.robots.txt 和 sitemap.xml 应该在/中找到,所以我的想法是为它们创建路由:

Is there any clever solution to store static files in Flask's application root directory. robots.txt and sitemap.xml are expected to be found in /, so my idea was to create routes for them:

@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
  response = make_response(open('sitemap.xml').read())
  response.headers["Content-type"] = "text/plain"
  return response

一定有更方便的东西:)

There must be something more convenient :)

推荐答案

最好的方法是设置static_url_path为root url

The best way is to set static_url_path to root url

from flask import Flask

app = Flask(__name__, static_folder='static', static_url_path='')

这篇关于Flask 中的静态文件——robot.txt、sitemap.xml (mod_wsgi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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