Re: accessing dictionary elements in opening tag of django template for loop
Thanks, John. Your second suggestion (that I find a way to modify the Document class) put me on the right track. I eventually found this article: http://blog.arbingersys.com/2008/04/google-app-engine-better-many-to-many.html which worked for my problem. On May 14, 7:17 pm, "John Lenton" <[EMAIL PROTECTED]> wrote: > On Wed, May 14, 2008 at 6:33 PM,John<[EMAIL PROTECTED]> wrote: > > > I'm having trouble accessing dictionary elements within a nested for > > loop. here are some code snippets: > > > [...] > > > # 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] > > right, templates are not python :) you can't do that, and it's on > purpose: that kind of fiddling around with the model is best done in > the view, not in the template. > > So... instead of doing e.g. > > documents = Document.objects.all() > tag_dict = {} > for document in documents: > tag_dict[document.uid] = DocumentTag.objects.filter(docid=document.uid) > > and passing those two into the template, you could do something along > the lines of > > documents = [dict(object=document, > tags=DocumentTag.objects.filter(docid=document.uid)) > for document in Document.objects.all()] > > and then in the template, you do > > {% for document in documents %} > {{ document.object.content }} > {% for doctag in document.tags %} {{ doctag.tagid }} {% endfor %} > {% endfor %} > > this is assuming there is some reason for you not to modify the > Document class to give it the appropriate methods or attributes to > access the related DocumentTag directly; if you could do that, life > would be much easier :) > > --JohnLenton ([EMAIL PROTECTED]) -- Random fortune: > The trouble with a lot of self-made men is that they worship their creator. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: accessing dictionary elements in opening tag of django template for loop
On Wed, May 14, 2008 at 6:33 PM, John <[EMAIL PROTECTED]> wrote: > > I'm having trouble accessing dictionary elements within a nested for > loop. here are some code snippets: > > [...] > > # 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] right, templates are not python :) you can't do that, and it's on purpose: that kind of fiddling around with the model is best done in the view, not in the template. So... instead of doing e.g. documents = Document.objects.all() tag_dict = {} for document in documents: tag_dict[document.uid] = DocumentTag.objects.filter(docid=document.uid) and passing those two into the template, you could do something along the lines of documents = [dict(object=document, tags=DocumentTag.objects.filter(docid=document.uid)) for document in Document.objects.all()] and then in the template, you do {% for document in documents %} {{ document.object.content }} {% for doctag in document.tags %} {{ doctag.tagid }} {% endfor %} {% endfor %} this is assuming there is some reason for you not to modify the Document class to give it the appropriate methods or attributes to access the related DocumentTag directly; if you could do that, life would be much easier :) -- John Lenton ([EMAIL PROTECTED]) -- Random fortune: The trouble with a lot of self-made men is that they worship their creator. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: accessing dictionary elements in opening tag of django template for loop
Thanks for the response, Norman! Unfortunately, the Google App Engine version of Django doesn't support the 'with' tag (which would otherwise solve my problem). Also, I don't think I can pass in a context var that already has the list I want, because I need a list *per* document. Any suggestions are extremely welcome! I've been banging my head against this problem all day. On May 14, 2:44 pm, "Norman Harman" <[EMAIL PROTECTED]> wrote: > Sorry, I don't know the exact answer but I can point you in right direction. > > templates always use dot notation for dictionary access > > tag_dict.document.uid > > But I don't think that will work inside a "for" tag and also won't work > cause you want the document.uid to be key not "document". > > The workaround I'm aware of is the "with" tag. > > http://www.djangoproject.com/documentation/templates/#with > > something like this > > {% with document.uid as uid %} >{% for doctag in tag_dict.uid %} {{ doctag.tagid }} {% endfor % } > {% endwith %} > > An alternative is to pass in a context var that already has the list you > want. > > -- > Norman J. Harman Jr. 512 912-5939 > Technology Solutions Group, Austin American-Statesman > ___ > Get out and about this spring with the Statesman! In print and online, > the Statesman has the area's Best Bets and recreation events. > Pick up your copy today or go to statesman.com 24/7. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---
Re: accessing dictionary elements in opening tag of django template for loop
Sorry, I don't know the exact answer but I can point you in right direction. templates always use dot notation for dictionary access tag_dict.document.uid But I don't think that will work inside a "for" tag and also won't work cause you want the document.uid to be key not "document". The workaround I'm aware of is the "with" tag. http://www.djangoproject.com/documentation/templates/#with something like this {% with document.uid as uid %} {% for doctag in tag_dict.uid %} {{ doctag.tagid }} {% endfor % } {% endwith %} An alternative is to pass in a context var that already has the list you want. -- Norman J. Harman Jr. 512 912-5939 Technology Solutions Group, Austin American-Statesman ___ Get out and about this spring with the Statesman! In print and online, the Statesman has the area's Best Bets and recreation events. Pick up your copy today or go to statesman.com 24/7. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~--~~~~--~~--~--~---