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

前端 未结 10 580
后悔当初
后悔当初 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.

    0 讨论(0)
  • 2020-11-29 16:08

    From the documentation here: http://flask.pocoo.org/docs/quickstart/#static-files

    Dynamic web applications need static files as well. That’s usually where the CSS and JavaScript files are coming from. Ideally your web server is configured to serve them for you, but during development Flask can do that as well. Just create a folder called static in your package or next to your module and it will be available at /static on the application.

    To generate URLs to that part of the URL, use the special 'static' URL name:

    url_for('static', filename='style.css')

    The file has to be stored on the filesystem as static/style.css.

    0 讨论(0)
  • 2020-11-29 16:09

    I'm having the same dilemma as well. Did some search and found my answer(MHO):

    Might as well quote from the documentation

    Dynamic web applications need static files as well. That’s usually where the CSS and JavaScript files are coming from. Ideally your web server is configured to serve them for you, but during development Flask can do that as well. Just create a folder called static in your package or next to your module and it will be available at /static on the application.

    IMHO: When your application is up for production, static file serving should be (or is ideally) configured on the webserver (nginx, apache); but during development, Flask made it available to serve static files. This is to help you develop rapidly - no need to setup webservers and such.

    Hope it helps.

    0 讨论(0)
  • 2020-11-29 16:11

    Serving static files has nothing to do with application that is meant to deliver dynamic content. The correct way of serving static files is dependent of what server you're using. After all, when you get your app up and running, you will need to bind it to a web server. I can speak only for apache httpd, so the way of serving static files is defined in the virtual host that you are binding to your application through mod-wsgi. Here is the guide that will show you how to serve sitemaps, robots.txt or any static content: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#Mounting_At_Root_Of_Site

    0 讨论(0)
提交回复
热议问题