Re: Unable to access Dictionary values in Template (object pk is the dict key)

2011-05-05 Thread Tom Evans
On Thu, May 5, 2011 at 12:11 PM, AJ  wrote:
> I see. Any clue where I can begin in my case (for filters etc.)? Thanks.
>

I disagree with the BDFLs on this, for me this pattern is very natural:

{% for key in list_of_keys %}
{{ some_dictionary[key] }}
{{ some_other_dict_with_same_keys[key] }}
{% endfor %}

Obviously this syntax is not allowed, and, as you've found out, key
lookup only works for numeric or string literals.

Therefore, I use a trivial filter tag:

{% load dict_tags %}
{% for key in list_of_keys %}
{{ some_dictionary|get:key }}
{{ some_other_dict_with_same_keys|get:key }}
{% endfor %}

some_app/templatetags/dict_tags.py:

from django import template
register = template.Library()
@register.filter
def get(the_dict, key):
  return the_dict[key]

Cheers

Tom

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Unable to access Dictionary values in Template (object pk is the dict key)

2011-05-05 Thread AJ
I see. Any clue where I can begin in my case (for filters etc.)? Thanks.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Unable to access Dictionary values in Template (object pk is the dict key)

2011-05-04 Thread Calvin Spealman
You need to construct your query in the view function to filter for
the comments you want. You can't do these sorts of things from the
template, and that is largely by design so that you keep your logic in
one place: your views, and your presentation in one place: your
template.

On Wed, May 4, 2011 at 7:56 PM, AJ  wrote:
> I have a Django View that constructs a dictionary to a template. I have seen
> similar questions but no one shows how to access the dictionary value in the
> template using the object pk as the key (in my case the keys are pks of the
> object).
>
> View code that constructs the dict:
>
> comment_uservote = {}
>   if not current_logged_user.is_anonymous():
>     for comment in comments_all:
>         try:
>             co_vote = Vote.objects.get(user=current_logged_user,
> comment=comment)
>             comment_uservote[comment.id] = co_vote.vote
>         except Vote.DoesNotExist:
>             co_vote = ''
>             comment_uservote[comment.id] = co_vote
>
> I have also tried with comment_uservote[str(comment.id)] but this does not
> help either.
>
> Template (that does not work):
>
> {% for comment in comments %}
>   {{comment_uservote.comment.pk}} 
> {% enfor %}
>
> However, the following works if I add any comment's pk to
> the comment_uservote.
>
> Template (that works but if a direct substitution):
>
> {% for comment in comments %}
>   {{comment_uservote.16}} 
> {% enfor %}
>
> Appreciate your help. Please let me know if you need something more from me.
> Thanks.
>
> --
> 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
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>



-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.