在Google App Engine外部使用webapp2提供样式表 [英] Serve stylesheet with webapp2 outside of Google App Engine
问题描述
我有幸通过静态文件访问静态文件这种方法,以及我使用google-fu实现了一个StaticFileHandler:
import os
import mimetypes
import webapp2
导入日志
$ b $ class StaticFileHandler(webapp2.RequestHandler):
def get(self,path):
abs_path = os.path.abspath(os.path.join (self.app.config.get('webapp2_static.static_file_path','static'),path))
if os.path.isdir(abs_path)或abs_path.find(os.getcwd())!= 0 :
self.response.set_status(403)
返回
尝试:
f = open(abs_path,'r')
self.response.headers.add_header('内容类型', mimetypes.guess_type(abs_path)[0])
self.response.out.write(f.read())
f.close()
除外:
self.response .set_status(404)
其中我的主要应用程序路由如下所示:
app = webapp2.WSGIApplication([('/',HelloWorld),
(r'/ display',DisplayHandler),
r'/ static /(.+)',StaticFileHandler)
],debug = True)
我的css文件位于应用程序根目录下的文件夹中:/static/css/main.css
我可以通过直接url访问该文件,甚至可以链接它作为样式表,但样式不适用。有任何想法吗?是否有另一种方式来服务样式表?一些实现类似GAE的app.yaml的方法?
你不需要一个静态文件处理程序。 >
将应用程序上传到您的 app.yaml
- url:/ static /
static_dir:static
文档位于以下位置: https://developers.google.com/appengine/docs / python / config / appconfig#Static_Directory_Handlers
编辑:
在下面的评论中看到答案
So I have successfully deployed an app using webapp2/jinja2 and a Paste server, but am having trouble serving static stylesheets.
I have had luck accessing static files via this method, as well as implementing a StaticFileHandler I found with some google-fu:
import os
import mimetypes
import webapp2
import logging
class StaticFileHandler(webapp2.RequestHandler):
def get(self, path):
abs_path = os.path.abspath(os.path.join(self.app.config.get('webapp2_static.static_file_path', 'static'), path))
if os.path.isdir(abs_path) or abs_path.find(os.getcwd()) != 0:
self.response.set_status(403)
return
try:
f = open(abs_path, 'r')
self.response.headers.add_header('Content-Type', mimetypes.guess_type(abs_path)[0])
self.response.out.write(f.read())
f.close()
except:
self.response.set_status(404)
where my main app routing looks like:
app = webapp2.WSGIApplication([('/', HelloWorld),
(r'/display', DisplayHandler),
(r'/static/(.+)', StaticFileHandler)
], debug=True)
My css files are in a folder under the app root: /static/css/main.css
I can access the file via direct url, and even link it as a stylesheet, but the styles won't apply. Any ideas? Is there another way to serve stylesheets? Some way to implement an app.yaml similar to GAE?
you dont need a static file handler.
upload the app with the static file folder by adding this to your app.yaml
- url: /static/
static_dir: static
docs are here: https://developers.google.com/appengine/docs/python/config/appconfig#Static_Directory_Handlers
edit: see answer below in comments
这篇关于在Google App Engine外部使用webapp2提供样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!