发送http帖子
import requests
payload = [{'name': 'pippo', 'age':'7'}, {'name':'luca', 'age':'12'}]
r = requests.post("http://127.0.0.1:8000", data=payload)
print(r.url)
在Django中接收Http帖子
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from home.models import Post
@csrf_exempt
def home(request):
all_post = Post.objects.all()
context = {'request_method': request.method}
if request.method == 'POST':
context['request_payload'] = request.POST.dict()
post_data = dict(request.POST)
print(post_data)
if request.method == 'GET':
context['request_payload'] = request.GET.dict()
return render(request, 'main/index.html', context, {'all_post':all_post})
在打印中(post_data),我看不到[{''name':'pippo','age':'7'},{'name':'luca','age':'12'}] >
为什么?我的错误在哪里?
答案 0 :(得分:0)
您应为要通过请求发布的数据命名。像这样:
payload = {'data': [{'name': 'pippo', 'age':'7'}, {'name':'luca', 'age':'12'}]}
当您想要获取数据时,请按以下步骤操作:
post_data = request.POST['data']