GAE Python: 本地无法加载静态文件,但在appspot上正常加载
我遇到了一个奇怪的问题,搜索了很多解决办法,但到目前为止,这个问题看起来是独一无二的。
我的问题是,当我在本地使用localhost运行应用时,我的静态文件(比如css、模板、javascript等)无法加载。但是,当我把应用部署到GAE服务器(appspot)上时,这些静态文件就能正常运行了!
这个问题甚至出现在GAE文档中的默认Guestbook应用上。
我在Windows上使用的是GAE启动器(Python)1.9.6,Python是64位版本,2.7.7。我已经确认GAE指向了正确的Python位置。
到目前为止,我在每个应用中都遇到了这个问题,所以我怀疑这不是代码的问题。不过,这里有一些代码供你参考:
app.yaml
application: ipeech
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: guestbook.application
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
guestbook.py
import os
import urllib
from google.appengine.api import users
from google.appengine.ext import ndb
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
MAIN_PAGE_FOOTER_TEMPLATE = """\
<form action="/sign?%s" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<hr>
<form>Guestbook name:
<input value="%s" name="guestbook_name">
<input type="submit" value="switch">
</form>
<a href="%s">%s</a>
</body>
</html>
"""
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
# We set a parent key on the 'Greetings' to ensure that they are all in the same
# entity group. Queries across the single entity group will be consistent.
# However, the write rate should be limited to ~1/second.
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return ndb.Key('Guestbook', guestbook_name)
class Greeting(ndb.Model):
"""Models an individual Guestbook entry."""
author = ndb.UserProperty()
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
'url': url,
'url_linktext': url_linktext,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
# We set the same parent key on the 'Greeting' to ensure each Greeting
# is in the same entity group. Queries across the single entity group
# will be consistent. However, the write rate to a single entity group
# should be limited to ~1/second.
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('content')
greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestbook),
], debug=True)
index.html
<!DOCTYPE html>
{% autoescape true %}
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
{% for greeting in greetings %}
{% if greeting.author %}
<b>{{ greeting.author.nickname() }}</b> wrote:
{% else %}
An anonymous person wrote:
{% endif %}
<blockquote>{{ greeting.content }}</blockquote>
{% endfor %}
<form action="/sign?guestbook_name={{ guestbook_name }}" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<hr>
<form>Guestbook name:
<input value="{{ guestbook_name }}" name="guestbook_name">
<input type="submit" value="switch">
</form>
<a href="{{ url|safe }}">{{ url_linktext }}</a>
</body>
</html>
{% endautoescape %}
index.yaml
索引:
# AUTOGENERATED
# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run. If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED"). If you want to manage some indexes
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
- kind: Greeting
ancestor: yes
properties:
- name: date
direction: desc
在“stylesheets”子文件夹内,main.css
body {
font-family: Verdana, Helvetica, sans-serif;
background-color: #DDDDDD;
}
谢谢!
3 个回答
0
如果你的 static
文件夹不在根目录下,而是在另一个文件夹里,比如说 app
(就像我这种情况),那么你需要在你的 app.yaml
文件中把它包含进去:
- url: /app/static
static_dir: static
secure: always
0
我认为这是Windows版GAE(Google App Engine)中的一个错误。当它在猜测静态文件的类型时,会把类型以一种Python的unicode字符串形式放在头部,这样就会导致静态文件处理器出现500错误。
可以看看以下最近的问题:
Google App Engine: 无法提供静态资源,出现以下错误:
请在GAE的错误追踪系统中报告这个问题。
1
请将以下内容替换:
- url: /stylesheets
static_dir: stylesheets
为:
- url: /(.*\.css)
static_files: static/\1
upload: static/.*\.css
希望这对你有用,就像对我有用一样。