|
List Info
Thread: getattr() does not work in py:with
|
|
| getattr() does not work in py:with |
  United States |
2007-07-19 06:55:55 |
Hi people,
I use Genshi with Django and I'm trying to write a macro
that
processes errors in forms. I have a tag in my template:
<error-list py:if="form.error_dict"
name="username" />
and a macro to process it:
<py:match path="//error-list"
py:with="field = getattr(form,
select(' name').__str__())">
<py:if test="field.errors"
py:replace="HTML(field.html_error_list())" />
</py:match>
When the macro processes the tag and tries to do
getattr(form,
'username') I get AttributeError: 'FormWrapper' object has
no
attribute 'username'.
But when I change the first line as follows:
<py:match path="//error-list"
py:with="field = form.username">
Everything works like a charm. Aren't obj.attr and
getattr(obj,
'attr') the same? I also tried to put it in <?python
?> inside
py:match and that seems to not to be processed at all.
I'm using Genshi 0.4.2 on Python 2.5.1
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: getattr() does not work in py:with |
  United States |
2007-07-19 15:38:48 |
I solved the problem in a different way. Django's
FormWrapper seems to
be a kind of chameleon (some
unexpected output of __repr__()
maybe?)
Here's the macro:
<py:match path="//error-list"
py:with="errors = form['error_dict']
[select(' name').__str__()]">
<ul py:if="errors">
<li py:for="err in errors"
py:content="err" />
</ul>
</py:match>
Provided you assign Django's oldforms FormWrapper object to
"form",
this macro will allow you to print validation errors with
simple and
elegant:
<error-list name="username" />
Now tell me, isn't it beautiful, istead of writing ugly:
{% if form.username.errors %}
{{ form.username.html_error_list }}
{% endif %}
And what's more, by modifying the macro, you can display the
errors in
ANY way.
I hope someone will find this useful.
Dan
On Jul 19, 1:55 pm, "Daniel Kvasnicka jr."
<daniel.kvasnicka... gmail.com> wrote:
> Hi people,
> I use Genshi with Django and I'm trying to write a
macro that
> processes errors in forms. I have a tag in my
template:
>
> <error-list py:if="form.error_dict"
name="username" />
>
> and a macro to process it:
>
> <py:match path="//error-list"
py:with="field = getattr(form,
> select(' name').__str__())">
> <py:if test="field.errors"
> py:replace="HTML(field.html_error_list())"
/>
> </py:match>
>
> When the macro processes the tag and tries to do
getattr(form,
> 'username') I get AttributeError: 'FormWrapper' object
has no
> attribute 'username'.
>
> But when I change the first line as follows:
>
> <py:match path="//error-list"
py:with="field = form.username">
>
> Everything works like a charm. Aren't obj.attr and
getattr(obj,
> 'attr') the same? I also tried to put it in <?python
?> inside
> py:match and that seems to not to be processed at all.
>
> I'm using Genshi 0.4.2 on Python 2.5.1
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: getattr() does not work in py:with |
  United States |
2007-07-19 16:26:36 |
On Jul 19, 1:38 pm, "Daniel Kvasnicka jr."
<daniel.kvasnicka... gmail.com> wrote:
> I solved the problem in a different way. Django's
FormWrapper seems to
> be a kind of chameleon (some
unexpected output of __repr__()
> maybe?)
It sounds like "form" is really a dict. Genshi
does some magic in
your expressions so "form.username" gets converted
to
"form['username']" if necessary (Django's
templates are similar), but
explicitly calling getattr(form, 'username') will bypass
this magic
and fail since there is no username attribute.
> Here's the macro:
>
> <py:match path="//error-list"
py:with="errors = form['error_dict']
> [select(' name').__str__()]">
> <ul py:if="errors">
> <li py:for="err in errors"
py:content="err" />
> </ul>
> </py:match>
The __xxx__ methods are not intended for explicit calling.
You should
use str(foo) instead of foo.__str__().
-- Matt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: getattr() does not work in py:with |
  United States |
2007-07-20 05:00:11 |
> Genshi does some magic in
> your expressions so "form.username" gets
converted to
> "form['username']" if necessary
Aaaaah, so that was it! Thanks for explanation, maybe it was
all just
a RTFM-problem. If not I think this should be in the docs.
> The __xxx__ methods are not intended for explicit
calling. You should
> use str(foo) instead of foo.__str__().
It seems to me much more object-oriented and my code is
better
understandable this way (at least for me . Are there
any specific
issues with this? (performance?)
Cheers,
Dan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: getattr() does not work in py:with |
  United States |
2007-07-20 16:25:31 |
On Jul 20, 3:00 am, "Daniel Kvasnicka jr."
<daniel.kvasnicka... gmail.com> wrote:
> > Genshi does some magic in
> > your expressions so "form.username" gets
converted to
> > "form['username']" if necessary
>
> Aaaaah, so that was it! Thanks for explanation, maybe
it was all just
> a RTFM-problem. If not I think this should be in the
docs.
"In addition, it is possible to access items in a
dictionary using
"dotted notation" (i.e. as if they were
attributes), and vice-versa
(i.e. access attributes as if they were items in a
dictionary):"
http://genshi.e
dgewall.org/wiki/Documentation/templates.html#template-expre
ssions-and-code-blocks
> > The __xxx__ methods are not intended for explicit
calling. You should
> > use str(foo) instead of foo.__str__().
>
> It seems to me much more object-oriented and my code is
better
> understandable this way (at least for me . Are there
any specific
> issues with this? (performance?)
str(foo) is the Python way of doing things. These methods
are meant
to be hooks and calling them directly may not produce the
same
behavior as calling the standard function that uses that
hook. For
example, str(foo) will fall back on the __repr__ method if
foo doesn't
have a __str__ implementation, but if you try to call
__str__ directly
you'll get an AttributeError. In general you don't want to
call these
methods outside the class's own methods, but it may be
necessary
inside some methods such as to call a super class's
__init__, or for
your __radd__ to call __add__.
-- Matt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|