python - 未定义错误: 'None' has no attribute 'user_id'

标签 python html google-app-engine jinja2 google-cloud-datastore

收到错误:

File "/base/data/home/apps/p~clubs-cs50/1.389022909265479577/templates/my_profile.html", line 11, in top-level template code
    {% if user.user_id() == person.identity %}
UndefinedError: 'None' has no attribute 'user_id'

当我尝试从 GAE 数据存储区获取我的个人资料信息的数据时。 (我在输入信息时遇到类似的问题?我在另一个问题中问过。)

它在某个时间点可以工作,但我不知道是什么破坏了它。 :/

Controller :

def get(self):
    people = Person.query()

    user = users.get_current_user()
    if user:
        url = users.create_logout_url(self.request.uri)
        url_linktext = 'Logout'
    else:
        url = users.create_login_url(self.request.uri)
        url_linktext = 'Login'

    template_values = {
        'user': user,
        'people': people
    }
    template= JINJA_ENVIRONMENT.get_template('templates/my_profile.html')
    self.response.write(template.render(template_values))

模板:

{% for person in people %}
  {% if person.name %}
    {% if user.user_id() == person.identity %}
      <h1>!!!!!!!!</h1>
    {% endif %}
  {% endif %}
  <br><h3>Name: {{ person.name }}</h3>
  <h3>Email: {{person.email}}</h3>
  <h3>Class Year: {{person.year}}</h3>
  <h3>Bio: {{person.bio}}</h3>
{% endfor %}

(其中 !!!!! 表示这是用户的信息,并且现在显示其他人的个人简介。)

非常感谢您的帮助!

最佳答案

也许改变这一行:

{% if user.user_id() == person.identity %}

对此:

{% if user and user.user_id() == person.identity %}

关于python - 未定义错误: 'None' has no attribute 'user_id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102567/

相关文章:

html - 如何定义像素?

html - 页内书签链接问题

javascript - new Map() 在处理 JSON 字符串时返回错误

python - isinstance 不适用于 AppEngine 中的 Decimal

python - matplotlib 直方图上的第一个和最后一个条出现移动

python - Google Cloud Functions - 如何对 AWS S3 存储桶进行身份验证?

python - 在 python 中 for 循环的最后一次迭代期间执行操作

python - 从 Pandas 数据框列更新 slqalchemy orm 列的正确方法是什么

google-app-engine - Java GAE maven配置问题?只是另一个验证错误

python - Tipfy session 管理