【问题标题】:'self' not defined, jinja2, appengine'self' 未定义,jinja2,appengine
【发布时间】:2012-06-25 16:45:51
【问题描述】:

错误:

self.response.out.write(template.render(template_values)) NameError: 名称“self”未定义

与标有#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

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

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

【问题讨论】:

    标签: google-app-engine jinja2


    【解决方案1】:

    从您发布的代码来看,出错的代码行(以及前面几行)似乎缩进不够远。

    get方法应该如下对齐:

    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
    

    【讨论】:

    • 我看到当我用谷歌搜索这个问题时,然后重新缩进...... MainPage 类下面的 get(self) 函数缩进了 4 个空格......请问您指的是哪几行?
    • 谢谢,新的错误,但至少它们在 index.html 中 :) 感谢您让我通过该方法 align!
    猜你喜欢
    • 2018-03-11
    • 2011-02-27
    • 2020-12-24
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多