CSS不适用于Google App Engine

时间:2012-07-27 03:14:30

标签: python css google-app-engine jinja2

我正在使用这些文件的官方GAE webapp2教程构建留言簿应用程序

的HelloWorld / app.yaml中

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /.*
  script: helloworld.app

libraries:
- name: jinja2
  version: "2.6"

helloworld / index.html

    <html>
 <head>
    <link  rel="stylesheet" type="text/css" 
    href="/stylesheets/main.css" />

  </head>
  <body>
    {% for greeting in greetings %}
      {% if greeting.author %}
        <b>{{ greeting.author }}</b> wrote:
      {% else %}
        An anonymous person wrote:
      {% endif %}
      <blockquote>{{ greeting.content|escape }}</blockquote>
    {% endfor %}

    <form action="/sign" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Sign Guestbook"/></div>
    </form>

    <a href="{{ url }}">{{ url_linktext }}</a>

  </body>
</html>

helloworld / helloworld.py helloworld / stylesheets / main.css 与教程中的相同。

import jinja2
import os

jinja_environment = jinja2.Environment (
    loader=jinja2.FileSystemLoader(os.path.dirname(_file_)))

import cgi
import datetime
import urllib
import webapp2

from google.appengine.ext import db
from google.appengine.api import users


class Greeting(db.Model):
  """Models an individual Guestbook entry with an author, content, and date."""
  author = db.StringProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


def guestbook_key(guestbook_name=None):
  """Constructs a Datastore key for a Guestbook entity with guestbook_name."""
  return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')


class MainPage(webapp2.RequestHandler):
    def get(self):
        guestbook_name=self.request.get('guestbook_name')
        greetings_query = Greeting.all().ancestor(
            guestbook_key(guestbook_name)).order('-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,
            'url': url,
            'url_linktext': url_linktext,
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.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')
    greeting = Greeting(parent=guestbook_key(guestbook_name))

    if users.get_current_user():
      greeting.author = users.get_current_user().nickname()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))


app = webapp2.WSGIApplication([('/', MainPage),
                               ('/sign', Guestbook)],
                              debug=True)

css

body {
  font-family: Verdana, Helvetica, sans-serif;
  background-color: #DDDDDD;
}

当我运行代码时,一切都按预期工作但css未应用

i)当我查看源代码时,我甚至没有看到css的标签 ii)当我查看日志时,我得到了这个奇怪的错误:

INFO     2012-07-27 02:59:11,921 dev_appserver.py:2904] "GET /favicon.ico HTTP/1.1" 404 

我很感激任何帮助。 谢谢!

4 个答案:

答案 0 :(得分:3)

在helloworld.py的第4行,你写了_file _但它应该是__file __

如果我做出改变,整件事对我有用。另外,我还获得了对css文件的GET请求。

INFO     2012-07-28 17:19:17,895 dev_appserver.py:2952] "GET /stylesheets/main.css HTTP/1.1" 200 -

答案 1 :(得分:0)

听起来您的测试服务器没有为您修改过的index.html服务,但是它提供的原始index.html没有CSS链接标记。或者它可能使用缓存页面。尝试清除缓存或重新启动服务器。或者确保正确保存更新。

答案 2 :(得分:0)

我遇到了同样的问题,其他团队成员也是如此。

起初我被告知要回到Google App Engine Launcher的1.6.2版本但是没有用,所以我通过终端部署(使用旧的常规update.py方法)并且它有效。

我猜GoogleAppEngine Launcher不适用于某些项目。

注意:我们使用的是旧版M / S版本的AppEngine。

答案 3 :(得分:0)

您在app.yaml内的样式表上调用时出现严重错误。试试这个,而不是:

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /stylesheets
  static_dir: stylesheets

# you missed this section. It allows all .css files inside stylesheets
- url: /stylesheets/(.*\.(css)) 
  static_files: stylesheets/\1
  upload: stylesheets/(.*\.(css))

- url: /.*
  script: helloworld.app

libraries:
- name: jinja2
  version: "2.6"