Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing default url to static-media in Flask

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.

like image 389
Nick Crawford Avatar asked May 11 '11 14:05

Nick Crawford


2 Answers

Override static_folder instead.

app = Flask(__name__, static_folder = '/web_media')
like image 77
101010 Avatar answered Nov 01 '22 14:11

101010


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.

like image 33
Philip Southam Avatar answered Nov 01 '22 15:11

Philip Southam