如何在没有模板的情况下从django视图模拟HTTP Post请求 [英] How to simulate a HTTP Post request from a django view without a template
问题描述
我正在撰写观点,不太热衷于立即编写模板。
I am writing views, not so keen to write templates right away.
但我需要通过提交帖子请求来测试我的程序。
But I need to test my program by submitting post requests.
我如何在django视图中模拟HTTP Post
How do i simulate HTTP Post from within a django view
我知道python的urllib2和httplib模块允许很多选项,但我正在寻找优雅地融入django视图的东西。
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?
更新:部分答案通过向我的应用程序发送POST来处理测试。如果我想POST到外部服务并处理POST响应,该怎么办?如何在不编写模板的情况下完成。
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.
推荐答案
Django内置了模拟客户端实用程序,可以模仿请求,就好像它们来自浏览器一样。如果您不需要完全模仿浏览器并且只想直接从测试中调用视图,请考虑使用 RequestFactory 。
Django has a built in mock Client utility that can mimic requests as if they are coming from a browser. If you don't need to fully mimic a browser and just want to invoke your views directly from your tests, consider using a RequestFactory instead.
这篇关于如何在没有模板的情况下从django视图模拟HTTP Post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!