如何使用django发送POST请求?

时间:2022-08-04 15:32:00

I dont want to use html file, but only with django I have to make POST request.

我不想使用html文件,但只有django我必须发出POST请求。

Just like urllib2 sends a get request.

就像urllib2发送一个get请求一样。

4 个解决方案

#1


29  

A combination of methods from urllib2 and urllib will do the trick. Here is how I post data using the two:

urllib2和urllib的方法组合将起到作用。以下是我使用这两种方式发布数据的方法:

post_data = [('name','Gladys'),]     # a sequence of two element tuples
result = urllib2.urlopen('http://example.com', urllib.urlencode(post_data))
content = result.read()

urlopen() is a method you use for opening urls. urlencode() converts the arguments to percent-encoded string.

urlopen()是用于打开网址的方法。 urlencode()将参数转换为百分比编码的字符串。

#2


27  

Here's how you'd write the accepted answer's example using python-requests:

以下是使用python-requests编写接受的答案示例的方法:

post_data = {'name': 'Gladys'}
response = requests.post('http://example.com', data=post_data)
content = response.content

Much more intuitive. See the Quickstart for more simple examples.

更直观。有关更简单的示例,请参阅快速入门。

#3


6  

The only thing you should look at now:

你现在唯一应该看的东西:

http://docs.python-requests.org/en/latest/

http://docs.python-requests.org/en/latest/

#4


4  

You can use urllib2 in django. After all, it's still python. To send a POST with urllib2, you can send the data parameter (taken from here):

你可以在django中使用urllib2。毕竟,它仍然是python。要使用urllib2发送POST,您可以发送数据参数(取自此处):

urllib2.urlopen(url[, data][, timeout])

urllib2.urlopen(url [,data] [,timeout])

[..] the HTTP request will be a POST instead of a GET when the data parameter is provided

[...]当提供数据参数时,HTTP请求将是POST而不是GET

#1


29  

A combination of methods from urllib2 and urllib will do the trick. Here is how I post data using the two:

urllib2和urllib的方法组合将起到作用。以下是我使用这两种方式发布数据的方法:

post_data = [('name','Gladys'),]     # a sequence of two element tuples
result = urllib2.urlopen('http://example.com', urllib.urlencode(post_data))
content = result.read()

urlopen() is a method you use for opening urls. urlencode() converts the arguments to percent-encoded string.

urlopen()是用于打开网址的方法。 urlencode()将参数转换为百分比编码的字符串。

#2


27  

Here's how you'd write the accepted answer's example using python-requests:

以下是使用python-requests编写接受的答案示例的方法:

post_data = {'name': 'Gladys'}
response = requests.post('http://example.com', data=post_data)
content = response.content

Much more intuitive. See the Quickstart for more simple examples.

更直观。有关更简单的示例,请参阅快速入门。

#3


6  

The only thing you should look at now:

你现在唯一应该看的东西:

http://docs.python-requests.org/en/latest/

http://docs.python-requests.org/en/latest/

#4


4  

You can use urllib2 in django. After all, it's still python. To send a POST with urllib2, you can send the data parameter (taken from here):

你可以在django中使用urllib2。毕竟,它仍然是python。要使用urllib2发送POST,您可以发送数据参数(取自此处):

urllib2.urlopen(url[, data][, timeout])

urllib2.urlopen(url [,data] [,timeout])

[..] the HTTP request will be a POST instead of a GET when the data parameter is provided

[...]当提供数据参数时,HTTP请求将是POST而不是GET