我正在使用这些文件从官方 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
我很感激这方面的任何帮助。谢谢!