python - 谷歌应用引擎: Users API acting oddly

标签 python django google-app-engine

我认为我错误地使用了 Users API:

class BaseHandler(webapp.RequestHandler):
   user = users.get_current_user()

   def header(self, title):
     if self.user:
        render('Views/link.html', self, {'text': 'Log out', 'href': users.create_logout_url('/')})
     else:
        render('Views/link.html', self, {'text': 'Log in', 'href': users.create_login_url('/')})

link.html:

<p>
    <a href="{{href}}">{{text}}</a>
</p>

有时有效,有时无效。我将连续点击“注销”链接10次,然后重新加载页面,它会将我重定向到'/'页。然后,奇怪的是,有一次我会被注销。登录失败的方式基本相同。这是怎么回事?

已解决 - 这有效:

class BaseHandler(webapp.RequestHandler):

    def __init__(self):
        self.user = users.get_current_user()

    def header(self, title):
        if self.user:
            render('Views/message.html', self, {'msg': "Welcome, %s" % self.user.nickname()})
            render('Views/link.html', self, {'text': 'Log out', 'href': users.create_logout_url('/')})
        else:
            render('Views/link.html', self, {'text': 'Log in', 'href': users.create_login_url('/')})

看起来我可以通过将实例变量引用为 self.var_name 来获得它们在函数中,但从未在类级别上声明它们。奇怪。

最佳答案

您将 users.get_current_user() 的结果存储在名为 user 的变量中,但随后您的 if 检查 self.user,这不是同一个变量。

使用相同的变量名,一切都应该没问题!

关于python - 谷歌应用引擎: Users API acting oddly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2406424/

相关文章:

python - 如何创建相关字段?

python - ClassName(object) 中的 super(ClassName, self) 有什么作用? (在 django 文档中使用)

java - 使用 GWT 在运行时获取奇怪的 ClassNotDefFoundException

google-app-engine - 谷歌云应用引擎部署失败

python - matplotlib 图表区域 vs 绘图区域

python - 在 Heroku 上使用 Memcache 时,我应该配置 Beaker 的 `session.lock_dir` 吗?

python - 失去与 MySQL 服务器的连接

python - 为什么对象的键是None?

python - Pandas 根据左侧的列重命名列

python - with语句后作为对象使用是否存在潜在的漏洞?