2

错误:

self.response.out.write(template.render(template_values)) NameError: name 'self' is not defined

与标记为 # ERROR 的行有关,还有其他注释:

#!/usr/bin/env python27

import cgi
import webapp2
import jinja2
import time
import datetime 
import urllib
#import cgitb; cgitb.enable()
import os
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import memcache

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class Visitor(db.Model): # I still need this with jinja2, yes? 
    name = db.StringProperty(required=1)
    mood = db.StringProperty(choices=["good","bad","fair"])
    date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp2.RequestHandler):
    def get(self): # ERROR HERE
        visitor_query = Visitor.all().order('-date') #not sure about query...need to get curent visitor's submitted form values (name, mood). no log-in in app. 
        visitor = visitor_query.fetch(1)

        template_values = {
            'visitor': visitor,
            'url': url, #not sure how this applies, just following tutorial
            'url_linktext': url_linktext,
        }

    localtime = time.localtime(time.time())
    mon = localtime[1] # MONTH
    h = localtime[3] # HOUR
    span = "morning" if h == range(5,14) else "afternoon" if h == range(17,7) else "evening" 
    if mon <= 3:
        var1 = "winter"
       # more variables in if/elif statement here...I call these variables from index.html...

#    name = self.request.get("name") # not sure if I need to define these variables here using jinja2...tutorial does not define entity properties in example. 
#    name = name.capitalize()
#   mood = self.request.get("mood")

    template = jinja_environment.get_template('index.html')
    self.response.out.write(template.render(template_values)) # ERROR HERE


class Process(webapp2.RequestHandler): 
    def post(self):
        name = self.request.get("name")
        name = name.capitalize()
        mood = self.request.get("mood")
        message = Visitor(name=name, mood=mood)
        if users.get_current_user():
            message.name = users.get_current_user() #not sure if I need users.get_current...no log-in required
        message.mood = self.request.get("mood")
        message.put()

        self.redirect("/")

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

应用程序.yaml:

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

handlers:
#-   url: /stylesheets/ # I read no static files allowed with jinja2...not sure how I'll handle CSS...
#    static_dir: stylesheets
-   url: /.*
    script: main.app

libraries:
-   name: jinja2 
    version: latest

index.yaml(所有这些都可以在没有 jinja2 的情况下工作......)

indexes:

-   kind: Visitor 
    ancestor: yes
    properties:
    -   name: name
    -   name: mood
    -   name: date
        direction: desc

另外,我已经将 jinja2 文件夹从 g00gle_appengine/lib 目录交替复制(未剪切)到我的应用程序目录文件夹,包括仅复制“jinja”文件夹(与使用 gdata atom 和 src 的类似方法一样......)我还安装了 python -jinja2,位于:/usr/share/doc/python-jinja2

我的 index.html 位于我的应用程序目录的“模板”目录中。提前感谢您让我继续前进。

4

1 回答 1

3

From the code you've posted, it looks like the erroring line of code (and the preceding few) aren't indented far enough.

The get method should be aligned as follows:

def get(self): # ERROR HERE
    visitor_query = Visitor.all().order('-date') #not sure about query...need to get curent visitor's submitted form values (name, mood). no log-in in app. 
    visitor = visitor_query.fetch(1)

    template_values = {
        'visitor': visitor,
        'url': url, #not sure how this applies, just following tutorial
        'url_linktext': url_linktext,
    }

    localtime = time.localtime(time.time())
    mon = localtime[1] # MONTH
    h = localtime[3] # HOUR
    span = "morning" if h == range(5,14) else "afternoon" if h == range(17,7) else "evening" 
    if mon <= 3:
        var1 = "winter"
    # more variables in if/elif statement here...I call these variables from index.html...

    #    name = self.request.get("name") # not sure if I need to define these variables here using jinja2...tutorial does not define entity properties in example. 
    #    name = name.capitalize()
    #   mood = self.request.get("mood")

    template = jinja_environment.get_template('index.html')
    self.response.out.write(template.render(template_values)) # ERROR HERE
于 2012-06-25T16:51:18.173 回答