这是Google Application Engine + Django中的变量访问错误吗?

时间:2021-11-11 23:11:31

Recently I've spotted a very disturbing issue.

最近我发现了一个非常令人不安的问题。

I've got the following python code:

我有以下python代码:

for cat in cats:
  cat.pages = ['apple', 'table', 'computer']

template_values = {
  'cats': cats
}

path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
self.response.out.write(template.render(path, template_values))

The index.html django template looks like this:

index.html django模板如下所示:

{% for cat in cats %}
   <div>{{ forloop.counter }}</div>
   <div>name: {{ cat.cat_name }}</div>
   <div>pages: {{ cat.pages|length }}<br>
{% endfor %}

When I'm running the code above locally with the GAE SDK, I've got the following example results:

当我使用GAE SDK在本地运行上面的代码时,我得到以下示例结果:

1.
name: sample1
pages: 3

2.
name: sample2
pages: 3

etc. I can even build a nested loop since I can access cat.pages inside the loop. However, when I upload this code to the AppEngine, I'll get the following results:

我甚至可以构建一个嵌套循环,因为我可以访问循环中的cat.pages。但是,当我将此代码上传到AppEngine时,我将得到以下结果:

1.
name: sample1
pages: 0

2.
name: sample2
pages: 0

And I can't even access the cat.pages variable at all. What's wrong with my code? Or is this a bug? It works locally as it's expected, but produces this strange result after deploying to GAE servers. Any help is appreciated.

我根本无法访问cat.pages变量。我的代码出了什么问题?或者这是一个错误?它按预期在本地工作,但在部署到GAE服务器后产生这种奇怪的结果。任何帮助表示赞赏。

2 个解决方案

#1


Maybe try:

for cat in cats:
   for item in ['apple', 'table', 'computer']:
       cat.pages.append(item)

If cat.pages is something funky in GAE like an instrumented list, your original code would have replaced it wholesale with a run-of-the-mill python list.

如果cat.pages在GAE中像一个仪表列表一样时髦,那么你的原始代码将用一般的python列表替换它。

#2


I think we're going to need more context to see what the problem is here. Where does 'cats' come from, and what are the individual cat objects? I'm guessing they're Model instances, but it's hard to say based on your example. Does it work if you replace them with an array of simple Object subclasses? This would show you if it's something specific to the class you're using.

我想我们需要更多的背景来看看问题在这里。 '猫'来自哪里,猫的个体对象是什么?我猜他们是模型实例,但根据你的例子很难说。如果用一组简单的Object子类替换它们,它是否有效?这将告诉您它是否是您正在使用的类的特定内容。

In general, though, it's a bad idea to assign arbitrary properties to your objects for templating convenience. You'd be better off creating a container object, or dictionary.

但是,一般情况下,为了方便起见,为对象分配任意属性是个坏主意。你最好创建一个容器对象或字典。

#1


Maybe try:

for cat in cats:
   for item in ['apple', 'table', 'computer']:
       cat.pages.append(item)

If cat.pages is something funky in GAE like an instrumented list, your original code would have replaced it wholesale with a run-of-the-mill python list.

如果cat.pages在GAE中像一个仪表列表一样时髦,那么你的原始代码将用一般的python列表替换它。

#2


I think we're going to need more context to see what the problem is here. Where does 'cats' come from, and what are the individual cat objects? I'm guessing they're Model instances, but it's hard to say based on your example. Does it work if you replace them with an array of simple Object subclasses? This would show you if it's something specific to the class you're using.

我想我们需要更多的背景来看看问题在这里。 '猫'来自哪里,猫的个体对象是什么?我猜他们是模型实例,但根据你的例子很难说。如果用一组简单的Object子类替换它们,它是否有效?这将告诉您它是否是您正在使用的类的特定内容。

In general, though, it's a bad idea to assign arbitrary properties to your objects for templating convenience. You'd be better off creating a container object, or dictionary.

但是,一般情况下,为了方便起见,为对象分配任意属性是个坏主意。你最好创建一个容器对象或字典。