如何使用Django发送POST请求? [英] How to send a POST request using django?

查看:617
本文介绍了如何使用Django发送POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想使用 html 文件,但只有django才需要发出POST请求。



就像 urllib2 发送 get 请求一样。

解决方案

结合使用urllib2和urllib中的方法即可解决问题。这是我使用这两个方法发布数据的方式:

  post_data = [('name','Gladys'),]#a两个元素元组的序列
result = urllib2.urlopen('http://example.com',urllib.urlencode(post_data))
content = result.read()

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

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

Just like urllib2 sends a get request.

解决方案

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

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.

这篇关于如何使用Django发送POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆