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 " {{form}} ".
> 
> 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-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to