0

我正在为我正在开发的应用程序进行联合登录。代码已经以不那么漂亮的方式工作了,但是当我尝试通过进行 in-html 迭代而不是迭代来美化事情时,html 我一直在收到这个错误。

  File "/home/wes/PycharmProjects/Unit3/templates/login.html", line 26, in top-level    template code
{% for p in new_providers.keys() %}
 UndefinedError: 'new_providers' is undefined

这是代码(提供者是一个单独的键:值字典)

new_providers = {}
for name in providers.keys():
    new_providers[name] = users.create_login_url(federated_identity=providers[name])


else:
     error = "You are not logged in"
     self.render("login.html", error=error, new_providers = new_providers)

最后,这里是渲染字典的 HTML 和 Jinja。这就是我怀疑有问题的地方,但对我来说似乎没有什么明显的......

{% for p in new_providers.keys() %}
        <a href="{{ new_providers[p] }}">{{ p }}</a>
        <br>
{% endfor %}
4

1 回答 1

0

好的,所以我不知道为什么会这样,但这是解决问题的更改(更改在 IF 条件的 self.render

原始代码:

class Login(Handler):
def get(self):
    user = users.get_current_user()

    if user:
        username = user.nickname()
        log_out = users.create_logout_url(self.request.uri)
        self.write(users.create_login_url(federated_identity='https://www.google.com/accounts/o8/id'))

        self.render("login.html", log_out=log_out, username=username)
    else:
        error = "You are not logged in"
        self.render("login.html", error=error, new_providers=new_providers)

修改后的代码

class Login(Handler):
def get(self):
    user = users.get_current_user()

    if user:
        username = user.nickname()
        log_out = users.create_logout_url(self.request.uri)
        self.write(users.create_login_url(federated_identity='https://www.google.com/accounts/o8/id'))

        self.render("login.html", log_out=log_out, username=username, **new_providers=new_providers**)
    else:
        error = "You are not logged in"
        self.render("login.html", error=error, new_providers = new_providers)
于 2013-01-08T18:48:18.803 回答