How to access underscore starting attributes in templates

2,055 views
Skip to first unread message

Fabio Natali

unread,
Sep 3, 2008, 12:28:34 PM9/3/08
to django...@googlegroups.com
Hi all of you.

I need to pass a model to my template and then access its
attributes. Some of them start with an underscore, which turns out to
be a problem for the template mechanism.

I think there are two major roads to solve this:

- write new template tags
- add a new method to each of my models (lot of new code to write?)

Are there any other alternatives? Which is the best bet? Could you
kindly point me out a few urls where I can study those topics?

Thank you very much,

--
Fabio Natali

Rajesh Dhawan

unread,
Sep 3, 2008, 3:11:06 PM9/3/08
to Django users
Hi Fabio,
The best practice in Django (and in Python too) is not to use
underscore-prefixed attributes outside of the class/module they are
defined in. Such attributes are typically meant to signify internally
used names that can change in future. They are similar to private or
non-public variables/methods in other languages. See:
http://www.python.org/dev/peps/pep-0008/

If you agree with this, your best bet is to revise your design so that
attributes that are publically used in templates, views, etc. are
named without the leading underscores. This will make your code much
more maintainable both by you and others. Alternatively, create a
method/property (your second solution above) to access each one of
them publically.

If you still want to go ahead with the template tag approach for a
short-term time saving (and long-term pain :), you can write one
template tag to give you back all such attributes on any of your
models. The code could be along these lines:

@register.simple_tag
def get_private_attribute(model_instance, attrib_name):
return getattr(model_instance, attrib_name, '')

You would use it like this in a template:

{% get_private_attribute model_object "_my_private_var_name" %}

-Rajesh D

Fabio Natali

unread,
Sep 3, 2008, 5:39:15 PM9/3/08
to django...@googlegroups.com
Dear Rajesh, thanks for your precious help.

Rajesh Dhawan scrisse:
[...]


> > I need to pass a model to my template and then access its
> > attributes. Some of them start with an underscore, which turns out to
> > be a problem for the template mechanism.

> Alternatively, create a method/property (your second solution above)


> to access each one of them publically.

I added a couple of methods to my models, so to access to the
underscore methods/attributes but with different names. I get an error
though.

TemplateSyntaxError at /
Caught an exception while rendering: unbound method __unicode__() must be called with Fornitore instance as first argument (got nothing instead)

I put some excerpts in http://dpaste.com/75743/. Can anybody see
what's missing/wrong?

Thanks.

> -Rajesh D

All the best,
Fabio.

--
Fabio Natali

bruno desthuilliers

unread,
Sep 3, 2008, 5:44:26 PM9/3/08
to Django users
On 3 sep, 23:39, Fabio Natali <nat...@poisson.phc.unipi.it> wrote:
(snip)
> I added a couple of methods to my models, so to access to the
> underscore methods/attributes but with different names. I get an error
> though.
>
> TemplateSyntaxError at /
> Caught an exception while rendering: unbound method __unicode__() must be called with Fornitore instance as first argument (got nothing instead)
>
> I put some excerpts inhttp://dpaste.com/75743/. Can anybody see
> what's missing/wrong?

Yeps : you're calling an instancemethod on a class. For what you're
trying to do, you need classmethods, ie:

@classmethod
def verbosename(cls):
return unicode(cls._meta.verbose_name)

@classmethod
def verbosenameplural(cls):
return unicode(cls._meta.verbose_name_plural)

HTH

Fabio Natali

unread,
Sep 4, 2008, 2:26:24 AM9/4/08
to django...@googlegroups.com
Dear Bruno, thank you very much for your help!!

bruno desthuilliers wrote:
[...]


> Yeps : you're calling an instancemethod on a class. For what you're
> trying to do, you need classmethods, ie:
>
> @classmethod
> def verbosename(cls):
> return unicode(cls._meta.verbose_name)

It did the trick! Now everything's fine. And I advanced a small step
into the Python world.

See ya,

--
Fabio Natali

Reply all
Reply to author
Forward
0 new messages