views:

1257

answers:

4

I am writing views, not so keen to write templates right away.

But I need to test my program by submitting post requests.

How do i simulate HTTP Post from within a django view

I am aware that urllib2 and httplib modules of python allow a lot of options, but I am looking for something that elegantly integrates into the django views.

Would U create a method that performs post, where would you call it from?

Update: Some of the answers deal with testing by sending a POST to my application. What if I want to POST to an external service and deal with POST response. How can I do it without writing templates.

+1  A: 

It sounds like you are looking for either a unit test or an acceptance test. Take a look at unittest which is part of the standard library.

For quick ad hoc tests while developing web apps, I like to use curl. It's a simple command line tool that easily generates all sorts of HTTP requests. You can POST with a command like:

curl -i -d field=value http://localhost:8080/sample/something

Curl is available on a lot of platforms. Check it out at http://curl.haxx.se/

Ken Fox
+1  A: 

If you are looking at this from the context of writing unittests, you could consider creating the Request object yourself and just calling the view function directly. You could even mock it, and any other parameters the view might take.

ironfroggy
+5  A: 

Django has a built in mock Client utility for doing this.

Joe Holloway
+1  A: 

To avoid the pain of creating the request object yourself you can use this tip on Django snippets

Steven Potter