Re: Using Tag Filters in a generic view

19 views
Skip to first unread message

Carlos Palol

unread,
Aug 24, 2012, 2:49:33 AM8/24/12
to django...@googlegroups.com, mdall...@gmail.com
Hi, please share your view class.

Cheers



On Thursday, 23 August 2012 18:56:37 UTC+2, miked wrote:
I have create a generic view and in that view I have specified context_object_name of errata_id_list in this case.  This generic view is tied to a query set of a model and in the view I specify the list of keys I would like to display.  Here is a snippit from the template:


{% for record in errata_id_list %}
<tr>
    {% for key in keys %}
        <td>{{record|dict_lookup:key}}</td>
    {% endfor %}
<</tr>
{% endfor %}

The template tag file dict_lookup is:

def dict_lookup(dict, key):
      return dict[key]
register.filter('dict_lookup',dict_lookup)


The odd thing is that the parameter "record" when passed to the template tag file seems to resolve to the primary key of the object.  I am expecting the parameter "record" to be a dictionary containing all fields within a record.

Is there anyway to get this within the template.

Melvyn Sopacua

unread,
Aug 24, 2012, 6:23:51 AM8/24/12
to django...@googlegroups.com
On 23-8-2012 18:56, miked wrote:

> The odd thing is that the parameter "record" when passed to the
> template tag file seems to resolve to the primary key of the object.

Nope, the expected thing is that record resolves to a model instance.

> I am expecting the parameter "record" to be a dictionary containing
> all fields within a record.

What part of the docs gave you that idea? Maybe it needs clarification.

> Is there anyway to get this within the template.

def attr_lookup(obj, attr):
return getattr(obj, attr, "")
register.filter('attr_lookup', attr_lookup)

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages