Alex Rivera | Logout

How to simulate a HTTP Post request from a django view without a template

Asked 2009-03-28T14:00:45.133
13

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.

Edit
Report

1 Answer

3

For such cases I think RequestFactory is ideally suited.

It works just like django's test client with the difference that it let's you create a request object that you can use anywhere. So you could just create your own request object and pass it to your view or form for testing.

I like this method of testing more then using the test client, since it comes closer to pure unit testing. That is, testing a single piece of code. If you're using the test client, there are more layers added before the actual code you're testing is reached.

answered 2012-05-03T21:28:18.177

Your Answer