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

前端 未结 5 1578
暖寄归人
暖寄归人 2021-02-13 19:11

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

5条回答
  •  遇见更好的自我
    2021-02-13 19:47

    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.

提交回复
热议问题