在django中从post请求接收数据的正确方法是什么。请求来自纯javaScript,使用fetch api。以下是我的代码段:
def change_group_name(request, pk):
if request.method == 'POST':
user = request.user
name = request.POST.get('name', False)
pk = request.POST.get('id', False)
print('==========================================')
print(name)
print('============================================')
当我尝试打印name
时出现错误
django.utils.dataStructures.MultiValueDictKeyError 'name'
javascript
async sendInfo(name ,id , csrf_token) {
fetch('http://127.0.0.1:8000/accounts/group/detail/10/settings/change',{
method: 'post',
credentials: "same-origin",
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'X-CSRFToken':csrf_token
},
body: {'id': JSON.stringify(id), 'name': JSON.stringify(name)}
})
}
下面是urlpattern
:
urlpatterns = [
url(r'^group/detail/(?P<pk>\d+)/settings/change$',change_group_name,
name='group_settings_change'),
]