我的Django项目出现了一些错误。
所以,我使用Django Rest Framework
class CompaniesInviteView(APIView):
def get(self, request, pk=None):
return Response({}, status=status.HTTP_204_NO_CONTENT)
def post(self, request, pk=None, **kwargs):
print request.POST
的url:
from django.conf.urls import patterns, url, include
from views import *
urlpatterns = patterns(
'',
url(r'^(?P<pk>\d+)/invite/?$', CompaniesInviteView.as_view(), name='companies'),
)
而且,当我发送POST请求时:
import requests
r = requests.Session()
data = {
"users": [
{
"first_name": "1",
"last_name": "1",
"email": "123@12.eu",
"password1": "123",
"password2": "123"
},
{
"first_name": "12",
"last_name": "12",
"email": "1234@12.eu",
"password1": "123",
"password2": "123" }
]
}
print data
reg = r.post('http://localhost:8000/api/companies/15/invite', data=data)
Django说我:
<QueryDict: {u'users': [u'password1', u'first_name', u'last_name', u'email', u'password2', u'password1', u'first_name', u'last_name', u'email', u'password2']}>
2015-12-08 18:09:31,393 DEBUG fus.utils params (101) GET: <QueryDict: {}>
2015-12-08 18:09:31,393 DEBUG fus.utils params (102) POST: <QueryDict: {u'users': [u'password1', u'first_name', u'last_name', u'email', u'password2', u'password1', u'first_name', u'last_name', u'email', u'password2']}>
所以我发送
{
"users": [
{
"first_name": "1",
"last_name": "1",
"email": "123@12.eu",
"password1": "123",
"password2": "123"
},
{
"first_name": "12",
"last_name": "12",
"email": "1234@12.eu",
"password1": "123",
"password2": "123" }
]
}
我得到
{u'users': [u'password1', u'first_name', u'last_name', u'email', u'password2', u'password1', u'first_name', u'last_name', u'email', u'password2']}
而且,我不知道为什么。 有人能帮我吗?也许有人知道,如何修复这个
答案 0 :(得分:1)
在请求中,您需要使用json
代替data
,例如:
reg = r.post('http://localhost:8000/api/companies/15/invite', json=data)
要访问帖子数据,您应该使用request.data
class CompaniesInviteView(APIView):
def get(self, request, pk=None):
return Response({}, status=status.HTTP_204_NO_CONTENT)
def post(self, request, pk=None, **kwargs):
print request.data