Static files in Flask - robot.txt, sitemap.xml (mod_wsgi)

前端 未结 10 585
后悔当初
后悔当初 2020-11-29 15:15

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

10条回答
  •  一整个雨季
    2020-11-29 16:08

    @vonPetrushev is right, in production you'll want to serve static files via nginx or apache, but for development it's nice to have your dev environment simple having your python app serving up the static content as well so you don't have to worry about changing configurations and multiple projects. To do that, you'll want to use the SharedDataMiddleware.

    from flask import Flask
    app = Flask(__name__)
    '''
    Your app setup and code
    '''
    if app.config['DEBUG']:
        from werkzeug import SharedDataMiddleware
        import os
        app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
          '/': os.path.join(os.path.dirname(__file__), 'static')
        })
    

    This example assumes your static files are in the folder "static", adjust to whatever fits your environment.

提交回复
热议问题