简体   繁体   English

GAE Python-使用全局变量字典作为视图元素

[英]GAE Python - using global variable dictionary for view elements

I can read and write from a global dictionary variable using a function. 我可以使用函数从全局字典变量进行读写。

dic = { 'someKey' : 'someVal' }
def f():
    dic['newKey'] = 'newVal'
    print dic['someKey']

I'm wanting to do the same thing in GAE Python inside a module 我想在模块中的GAE Python中执行相同的操作

class MainPage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if not user:
            viewValues = {}
            viewValues['login'] = (users.create_login_url(self.request.path))
            self.response.out.write(template.render('view/login.html', viewValues))
        else:
            self.redirect("/")

    viewValues = {'promotion' : [
                                {'icon': 'time_ico.png',
                                 'heading': 'Instant feedback.',
                                 'detail': 'Quiz your students and get instant results!</br>No need to waste time checking answers manually.'},
                                {'icon': 'money_ico.png',
                                 'heading': 'No equipment necessary!',
                                 'detail': 'No need to spend money on special equipment.</br>Use studyWise&copy; free of charge now.'},
                                {'icon': 'cell_ico.png',
                                 'heading': 'Accessible.',
                                 'detail': 'Can be used with any smartphone or laptop, no installation required.'},
                                {'icon': 'statistics_ico.png',
                                 'heading': 'Statistics.',
                                 'detail': 'Get helpful info about your students understanding of the learning material today.'}
                             ]
              }


Traceback (most recent call last):
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__
    handler.get(*groups)
  File "C:\Current Project\StudentVoice_Refactor\src\login.py", line 14, in get
    viewValues['login'] = (users.create_login_url(self.request.path))
NameError: global name 'viewValues' is not defined

Why is this happening? 为什么会这样呢? Thanks 谢谢

The way it is done in python is by calling self.varName 在python中完成的方式是调用self.varName

In AS3/Java you could go either way AFAIK, you could write memberName or this.memberName 在AS3 / Java中,您可以采用AFAIK的任何一种方式,可以编写memberName或this.memberName

But in python you cannot, one must use self.memberName and not memberName. 但是在python中,您不能,必须使用self.memberName而不是memberName。

Well, guess that solves it. 好吧,猜想就解决了。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM