我刚刚搭建了一个带有Django的Apache服务器,并为了测试它,在views.py中编写了一个非常简单的函数。
channel = rabbit_connection()
@csrf_protect
@csrf_exempt
def index(request):
data={'text': 'Food truck is awesome! ', 'email': 'bob@yahoo.com', 'name': 'Bob'}
callback(json.dumps(data))
context = RequestContext(request)
return render_to_response('index.html', context_instance=context)
如果我向服务器发送GET
或POST
请求,这个函数可以正常工作。但是我想从POST
请求中获取这些数据。假设我像这样发送请求:
import pycurl
import simplejson as json
data = json.dumps({'name':'Bob', 'email':'bob@yahoo.com', 'text': u"Food truck is awesome!"})
c = pycurl.Curl()
c.setopt(c.URL, 'http://ec2-54-......compute-1.amazonaws.com/index.html')
c.setopt(c.POSTFIELDS, data)
c.setopt(c.VERBOSE, True)
for i in range(100):
c.perform()
我希望你能在视图中展示像这样的内容:
我想要在视图中展示以下内容:
if request.method == 'POST':
data = ?????? # Something that will return me my dictionary
提醒一下: 它始终将以JSON格式存在,而字段是未知的。
name = request.POST.get('name','')
将返回变量name
的值为 "Bob"。 - JcKelley