我只設置了Django的Apache服務器,並對其進行測試,在views.py如何從請求中獲取POST數據?
channel = rabbit_connection()
@csrf_protect
@csrf_exempt
def index(request):
data={'text': 'Food truck is awesome! ', 'email': '[email protected]', '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':'[email protected]', '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格式和領域是未知的。
嗨@JcKelley,據我瞭解上面的語句將得到的值數據?正確?但是我的JSON中沒有價值數據 – Vor
這會從字典中獲取關鍵值。所以'name = request.POST.get('name','')'會返回'name'變量「Bob」 – JcKelley
正好,但是如何返回整個字典呢? – Vor