dict variable in template

1 view
Skip to first unread message

Jason

unread,
Jul 4, 2008, 10:20:49 PM7/4/08
to Django developers
Hello,

I'm a newbie and using Django in the context of Google App Engine. In
my Python code I have:
url = users.create_login_url(self.request.uri)
data['attribute1'] = 10
data['attribute2'] = 20
formdata = {
'data': data,
'url': url,
'attribute_list': [attribute1, attribute2]
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, formdata))

In index.html I have:
{% for attribute in attribute_list %}
<p>{{ attribute }} <input type="text" name="{{ attribute }}"
value="{{ data.attribute }}">
{% endfor %}

The resulting HTML is:
<p>atttribute1 <input type="text" name="attribute1" value="">
<p>atttribute2 <input type="text" name="attribute2" value="">

What I am wanting is for the 10 and the 20 to appear in the value
attribute of those text boxes.

Russell Keith-Magee

unread,
Jul 4, 2008, 10:25:20 PM7/4/08
to django-d...@googlegroups.com
On Sat, Jul 5, 2008 at 10:20 AM, Jason <jsf8...@gmail.com> wrote:
>
> Hello,
>
> I'm a newbie and using Django in the context of Google App Engine.

Hi Jason,

1) Django-developers is a mailing list for discussing the development
of django itself, not for general user queries. Questions like this
one should be asked on django-users.

2) Given that your question is about Google App Engine, you may have
more luck asking in discussion group specific to App Engine. There are
some subtle differences between Google App Engine and Django, so an
answer which is completely correct for Django may be incorrect for App
Engine.

Yours,
Russ Magee %-)

Malcolm Tredinnick

unread,
Jul 4, 2008, 10:25:43 PM7/4/08
to django-d...@googlegroups.com

Django doesn't do indirect lookups like that. The thing after the dot in
{{{ data.attribute }} has to be the name of an attribute, not the name
of a variable that is used to look up the name of an attribute. You
could write a filter to do the indirect lookup for you (I believe there
might even be one in djangosnippets.org), but almost always you'll find
the cleaner solution is not to construct your data that way.

In this case, you can just as easily write:

{% for name,value in data.items %}
...
{% endfor %}

Alternatively, you could make data be a list of tuples (which would be a
better data structure if you want the results in a well-defined order,
since dictionary orderings will change as you add more things).

Regards,
Malcolm


Malcolm Tredinnick

unread,
Jul 4, 2008, 10:28:23 PM7/4/08
to django-d...@googlegroups.com
And I only just noticed you posted to django-dev instead of
django-users. This isn't the right list for that. Apologies for
answering your question.

Malcolm

Jason

unread,
Jul 4, 2008, 10:39:40 PM7/4/08
to Django developers
Thanks for the replies, everyone. I double-checked my message to make
sure it would be clear. Wonderful! And yet, I posted to the wrong
group :( If I have a follow-up I'll post in the users forum or the
Google App Engine forum.

On Jul 5, 2:25 am, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

Dave Smith

unread,
Jul 4, 2008, 11:10:07 PM7/4/08
to django-d...@googlegroups.com
On Fri, Jul 4, 2008 at 7:39 PM, Jason <jsf8...@gmail.com> wrote:

Thanks for the replies, everyone.  I double-checked my message to make
sure it would be clear.  Wonderful!  And yet, I posted to the wrong
group :(  If I have a follow-up I'll post in the users forum or the
Google App Engine forum.

There's also an #appengine channel on irc.freenode.net (and, if your question
is specific to Django, there's also a #django channel). Oft times these will
get you immediate answers.

Dave


Reply all
Reply to author
Forward
0 new messages