11

I've made a website using Flask and I have no problems getting things to work properly on the built-in development server. I've also been able to get things running on my production server under mod_wgsi. However, I host my static media from a static/CGI/PHP-5.2 application and I can't get Flask to 'see' it without manually changing all the urls in my html files.

The problem seems to be that the basic Flask setup expects static files to be within the flask application. See here for details. Essentially, I think I need to change the url of 'static' portion of the following one liner:

<link rel="stylesheet" href="{{url_for('static', filename='css/print.css')}}" type="text/css" media="print"/> 

It looks like I can change this in init.py, instructions here, but defining the static_path as follows doesn't seem to work.

app = Flask(__name__, static_path = '/web_media')

To be clear, if I manually define my url like this:

<link rel="stylesheet" href="/web_media/css/print.css" type="text/css" media="print"/>

everything works fine. Any help would be greatly appreciated.

Nick Crawford
  • 5,086
  • 2
  • 21
  • 20

3 Answers3

9

Override static_folder instead.

app = Flask(__name__, static_folder = '/web_media')
101010
  • 14,866
  • 30
  • 95
  • 172
4

If this is a production set up Flask should not be serving your static content; the web server (nginx, apache, cherokee, etc...) should be handling that as it is more efficient at handling those types of operations than the python process. From the sounds of it (based on the mod_wsgi reference) you are using apache, so this is how you could alter your config file to serve static content from static/CGI/PHP-5.2 using apache.

Assuming "web_media" is a directory under the somewhat fictitious /var/www/static/CGI/PHP-5.2 directory and contains your css/js/etc. assets. In your config file, within the area where you configure this app add something along the lines of.

Alias /web_media/ /var/www/static/CGI/PHP-5.2/web_media/

<Directory /var/www/static/CGI/PHP-5.2/web_media>
  Order deny,allow
  Allow from all
</Directory>

It's been a while since I used/configured apache, so no guarantees that the above will work perfectly the first time. But the main point is, use the webserver to handle the static media.

Philip Southam
  • 15,843
  • 6
  • 28
  • 20
3

For production environment you should serve your static files via http server (e.g. nginx/apache). See answer of Philip Southam above.

Nonetheless, there are times when you use werkzeug server (e.g. dev/testing). In that case, there are 2 keyword arguments to look at:

  • static_folder: physical location on the file system. If it is relative path than it is relative to the project root directory.
  • static_url_path: this is the url lookup path for static content.

For example, if I have a static file located at /path/to/static/files/myfile.jpg, with /path/to/static/files as static file root and you want to access it via http://example.com/mystatic/myfile.jpg. I will do the following:

from flask import Flask

app_name = __name__
app = Flask(app_name, static_folder='/path/to/static/files', static_url_path='/mystatic')

Assuming you are using flask version 0.7 or above (currently 0.11.1)

berniey
  • 2,772
  • 1
  • 18
  • 8