How can I do POST request in django?

我的梦境 提交于 2019-12-25 17:38:52

问题


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)

Anyone knows?

note: I'm using python 2.7


回答1:


You didn't set url.

data = {
  "username": "user",
  "password": "pass",
}

URL = 'http://example.com'
r = requests.post(URL, data=data)



回答2:


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()


来源:https://stackoverflow.com/questions/48751113/how-can-i-do-post-request-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!