如何在Django中进行POST请求? [英] How can I do POST request in django?
本文介绍了如何在Django中进行POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了 requests 之外,还有其他方法可以执行POST HttpRequest吗?
我只能使用DJANGO LIBS,因此不能import requests
.
with exception of requests, are there other ways for doing a POST HttpRequest?
I CAN ONLY USE DJANGO LIBS, so I cannot import requests
.
尤其是,我想在发布请求中传递用户名和密码.像这样:
In particular, I would like to pass the username and the password in the post request. something like:
data = {
"username": "user",
"password": "pass",
}
r = request.POST( data )
(请注意,此代码仅是示例)
(please note that this code is just an example)
有人知道吗?
注意:我正在使用python 2.7
note: I'm using python 2.7
推荐答案
如果无法使用请求,请尝试使用urllib2和urllib. 您对此有何看法?
If you cannot use requests, try using urllib2 and urllib. What do you think about this?
post_data = {
"username": "user",
"password": "pass",
}
result = urllib2.urlopen('http://example.com', urllib.urlencode(post_data))
content = result.read()
这篇关于如何在Django中进行POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文