在Flask中将默认url更改为static-media [英] Changing default url to static-media in Flask

查看:241
本文介绍了在Flask中将默认url更改为static-media的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Flask 创建了一个网站,我没有任何问题可以在内置的开发服务器上正常工作。我也能在mod_wgsi下的生产服务器上运行。但是,我从一个静态/ CGI / PHP-5.2 应用程序托管我的静态媒体,我无法获得Flask看看它没有手动改变我的html文件中的所有网址。

问题似乎是基本的Flask设置期望静态文件在烧瓶应用程序。有关详细信息,请参阅此处。基本上,我想我需要改变下面一行的'静态'部分的网址:

 < link rel = stylesheethref ={{url_for('static',filename ='css / print.css')}}type =text / cssmedia =print/> 

看起来我可以在 init .py, 这里,但是如下定义static_path似乎不起作用。

  app = Flask(__ name__,static_path ='/ web_media')

要清楚的是,如果我手动定义我的网址是这样的:

 < link rel =stylesheethref =/ web_media / css / print.csstype =text / cssmedia =print/> 

一切正常。任何帮助将不胜感激。

解决方案

如果这是一个生产设置,Flask不应该为您的静态内容提供服务; Web服务器(nginx,apache,cherokee等)应该能够处理这些,因为它比python进程更有效地处理这些类型的操作。从它的声音(基于mod_wsgi引用)你正在使用apache,所以这就是你如何修改你的配置文件来使用apache从静态/ CGI / PHP-5.2提供静态内容。 / p>

假设web_media是一个虚构的/var/www/static/CGI/PHP-5.2目录下的一个目录,包含了你的css / js / etc。资产。在你的配置文件中,在你配置这个应用程序的区域内添加一些内容。
$ b

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

< Directory /var/www/static/CGI/PHP-5.2/web_media>
顺序拒绝,允许
允许所有
< / Directory>

使用/配置apache已经有一段时间了,第一次。但重点是,使用Web服务器来处理静态媒体。

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.

解决方案

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.

这篇关于在Flask中将默认url更改为static-media的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆