List Info

Thread: JSON-ifying newform errors?




JSON-ifying newform errors?
country flaguser name
Spain
2007-05-30 16:40:32
Hey everyone,


I was thinking that I'd like to know if there is a standard
way of 
transforming a newforms reply to JSON (or anything else).

I'd like to think it would be easy to have a new-form,
submit it via 
JSON and be able to -automatically- display the errors, as
if you would 
just have called " {} ".

Of course one could return str(form) in the response, but in
my case 
it's not HTTP but rather flash that needs to interpret the
results (the 
errors etc) so I need some JSON love.

Of course I can roll my own and it's not too hard in this
case, but! Why 
reinvent the wheel 

cheerio,

  - bram

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: JSON-ifying newform errors?
country flaguser name
Australia
2007-05-30 21:06:06
On Wed, 2007-05-30 at 23:40 +0200, Bram - Smartelectronix
wrote:
> Hey everyone,
> 
> 
> I was thinking that I'd like to know if there is a
standard way of 
> transforming a newforms reply to JSON (or anything
else).
> 
> I'd like to think it would be easy to have a new-form,
submit it via 
> JSON and be able to -automatically- display the errors,
as if you would 
> just have called " {} ".
> 
> Of course one could return str(form) in the response,
but in my case 
> it's not HTTP but rather flash that needs to interpret
the results (the 
> errors etc) so I need some JSON love.
> 
> Of course I can roll my own and it's not too hard in
this case, but! Why 
> reinvent the wheel 

The intended solution (by the newforms design) to
requirements like this
is that you just write a new output method in your Form
subclass. You
are essentially replacing _as_html(). The logic in that
method for how
to pull together the various pieces of information you need
should be
fairly clear from reading the code. Actually, _as_html()
looks a bit
more complex that it is for most uses, because it is
intended to handle
a variety of formatted output and options like errors on
their own or
inline. So the lines of code that pull out the information
are not very
many.

I think it would better to override _as_html() -- or write a
method with
a new name that does the same thing -- in your subclass,
rather than
trying to mess around passing the right format string to
_as_htm. You
already have a good tool (the simplejson) for converting
data structures
to JSON, so concentrate on constructing the right data
structure, rather
than worrying about having to debug the object -> string
conversion
process.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )