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

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

问题描述

有什么聪明的解决方案将静态文件存储在Flask的应用程序根目录中。
robots.txt和sitemap.xml预计将在/中找到,所以我的想法是为他们创建路线:

  def sitemap():
response = make_response(open('sitemap.xml')。read ())
response.headers [Content-type] =text / plain
返回响应

必须有一些更方便的方法:

解决方案

最好的方法是设置 static_url_path 到根URL

  from flask import Flask 
$ b $ app = Flask(__ name__, static_folder ='static',static_url_path ='')


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 :)

解决方案

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天全站免登陆