Tuesday, June 29, 2010

Re: How To Create POST data without an actual POST?

> I'd like to find a way to let my users submit form data without
> submitting
> it via an actual web form. For example, I have users that would like
> to
> send their data via email, or perhaps provide it in an excel spread
> sheet,
>
> I'm trying to figure out how to do this and still leverage all the
> error
> checking associated with my forms (ie, via is_valid).
>
> I'm thinking that on the server side I could create a form with
> initial data,
> and then instead of rendering that form, I'd like to just convert it
> directly
> to a POST data dict, as if the user submitted the form without making
> any changes. Then I'd take that POST data, add a bit of additinal
> data
> that I've gotten from some other source (ie from email or excel), then
> run
> is_valid(), then do my standard save to my db object.
>
> Can anyone comment on if this seems like a good approach for doing
> this,
> and if so, if there is any django support for turning a form directly
> into a data dict?
>
> Thanks!
> Margie

Well it depends how you want to integrate your system.

In simplest form you can pass data as a plain python dictionary (which it
pretty much is) to form and validate.

my_data = { 'field1' : 1, 'field2' : 'some data', }

form = MyForm(data=my_data)

Works pretty well, there is no problems with that.

Or like someone already suggested you can do various middle formats (like
json) or send even "real" request from parser.

You could do even custom command that accepts "post data" as json and pushes
it to database through the form you've created.

Even forms normally take in request.POST using them is not limited to plain
HTTP POST requests but you can do it with plain dicts as well.

--

Jani Tiainen

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment