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)
app.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
此外,我交替地從g00gle_appengine/lib目錄複製(不是剪切)jinja2文件夾到我的應用程序目錄文件夾,包括只複製「jinja」文件夾(類似的方法使用gdata原子& src ...)還安裝了python-jinja2,它位於:/usr/share/doc/python-jinja2
我的index.html位於我的應用程序目錄中的「templates」目錄中。預先感謝讓我去。
我看到,當我用Google搜索這個問題,然後重新縮進......以下類的的MainPage GET(個體經營)功能縮進4個空格..請你準確地提到哪一行? – p1nesap
我編輯了我的答案以顯示正確的對齊方式。 – Scroog1
謝謝,新錯誤,但至少他們在index.html :)謝謝你讓我通過該方法對齊! – p1nesap