GAE-Python:静态文件不在localhost中加载,但在app部署到appspot上时可以很好地加载
我遇到了一个奇怪的问题,正在寻找解决方案,但到目前为止,它似乎是独一无二的 我的问题是,在GAE中使用localhost运行应用程序时,我的静态文件(css、模板、javascript等)没有加载。然而,当我将应用程序部署到GAE服务器(appspot)上时,这些静态文件运行得非常好 这甚至发生在GAE文档中的默认留言簿应用程序中 我正在Windows上使用GAE launcher(Python)1.9.6。Python是64位版本,2.7.7。已经确保GAE指向python的正确位置 到目前为止,我在每个应用程序中都遇到了这个问题,所以我怀疑这是代码的问题。但是,以下代码供您参考: app.yamlGAE-Python:静态文件不在localhost中加载,但在app部署到appspot上时可以很好地加载,python,google-app-engine,static,localhost,Python,Google App Engine,Static,Localhost,我遇到了一个奇怪的问题,正在寻找解决方案,但到目前为止,它似乎是独一无二的 我的问题是,在GAE中使用localhost运行应用程序时,我的静态文件(css、模板、javascript等)没有加载。然而,当我将应用程序部署到GAE服务器(appspot)上时,这些静态文件运行得非常好 这甚至发生在GAE文档中的默认留言簿应用程序中 我正在Windows上使用GAE launcher(Python)1.9.6。Python是64位版本,2.7.7。已经确保GAE指向python的正确位置 到目前为
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
留言簿
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)
在子文件夹“样式表”main.css中
body {
font-family: Verdana, Helvetica, sans-serif;
background-color: #DDDDDD;
}
谢谢大家! 我认为这是GAE for Windows中的一个错误,在猜测静态文件的mimetype时,将头中的mimetype设置为python unicode字符串,在静态文件处理程序中抛出500个错误 见以下最近的问题: 请在GAE错误跟踪器中报告。请更换:
- url: /stylesheets
static_dir: stylesheets
与:
如果您的
静态
文件夹不在您的根目录中,而是在另一个文件夹中,例如应用程序
(如我的情况),您需要将其包括在您的应用程序中。yaml
:
- url: /app/static
static_dir: static
secure: always
我看不出有错。但是您可以使用它进行测试:1)您可以键入静态目录资源的url来获取静态文件。2) 您可以使用chrome调试器查看请求、响应和错误。
- url: /stylesheets
static_dir: stylesheets
- url: /(.*\.css)
static_files: static/\1
upload: static/.*\.css
- url: /app/static
static_dir: static
secure: always