0

我正在尝试设置一个包含对象列表、对象属性名称和对象属性值的调试页面。我正在尝试获取特定对象类型的特定属性的值。当我编码时,对象类型或属性都不知道。

以下是我为之准备的相关部分:

在我的 test.py

if self.request.get('objID'):
  qGet = self.request.get
  thisObj = db.get(db.Key(qGet('objID')))

template_values = { 'thisObj' : thisObj }

template = JINJA_ENVIRONMENT.get_template('objProp.html')
self.response.write(template.render(template_values))

在我的 objProp.html 模板中

{% if thisObj %}
  <ul>List of properties
  {% for p in thisObj.properties() %}
    <li>{{ p }} : {{ thisObj.p }}</li>
  {% endfor %}
  </ul>
{% endif %}

然而,由于 thisObj 中没有属性 p 它总是打印出一个空值,我真的想要打印出在循环中的特定点处 p 所指的任何属性的值

任何帮助将不胜感激!

4

2 回答 2

1

这是我开始使用的一种方法。我不会接受它,因为我还没有认为它是一个“好”的方法。

另请参阅:Google App Engine:如何以编程方式访问模型类的属性?

这个问题让我大部分时间都在那里,这是我正在使用的,它似乎运行正常:

{% if thisObj %}
  <ul>List of properties
  {% for p in thisObj.properties() %}
    <li>{{ p }} : {{ thisObj.properties()[p].get_value_for_datastore(thisObj) }}</li>
  {% endfor %}
  </ul>
{% endif %}

似乎 p 正在解析为字符串,thisObj.properties()[p]正在返回对象属性,然后我只需要从该对象中获取值.get_value_for_datastore(thisObj)

从此处的文档中引用:属性类

于 2013-05-07T04:00:39.417 回答
0

您不应该使用thisObj.p ,因为您没有在thisObj. .p在这种情况下,不是循环中的 p,而是试图引用名为“p”的属性或方法。

你应该使用

getattr(thisObj,p)

于 2013-05-07T03:46:10.103 回答