Hello Django Users,
I'm having trouble accessing dictionary elements within a nested for
loop. here are some code snippets:
# here is the python code that sets up the page
class DocumentPage(webapp.RequestHandler):
def get(self):
# get a list of documents from google app engine datastore
documents = Document.all().order('-date')
# for every document, get it's associated taglist and stick that
in a dictionary
tag_dict = {}
for document in documents:
tag_dict[document.uid] = DocumentTag.all().filter('docid
=',document.uid)
template_values = {
'documents': documents,
'tag_dict': tag_dict
}
path = os.path.join(os.path.dirname(__file__), 'document.html')
self.response.out.write(template.render(path, template_values))
# so far so good, but then I try to iterate over the documents and
their associated tags in a template
# here is the template with the html stripped out for readability
{% for document in documents %}
{{ document.content }}
{% for doctag in tag_dict[document.uid] %} {{ doctag.tagid }} {%
endfor % }
{% endfor %}
# document.content is fine, but when I try to access tag_dict within
the template's for loop, it complains:
TemplateSyntaxError: Could not parse the remainder: [document.uid]
# I've tried running essentially the same code in python (without
touching django templates), and it seems fine. So there's something
about django templates that doesn't accessing a particular dictionary
element within the opening tag of a for loop. I'm aware that there is
a hack whereby one accesses dict.items, but that seems to defeat the
whole purpose of a dictionary, which is to quickly access a particular
element.
# Any help would be greatly appreciated!!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---