我正在制作网络服务,用于发布智能手机的评论,以下是我的代码
@api_view(['POST'])
def comment_post(request,newsId=None):
data = json.loads(request.body)
responseData= dict({
"result": list()
})
if(newsId):
commentNews = models.Comments.objects.create()
commentNews.comment_description = data.get('comment_description').strip()
commentNews.like_count = int(data.get('like_count'))
commentNews.user_name = data.get('user_name').strip()
commentNews.user_email_id = data.get('user_email_id').strip()
commentNews.parent_comment = data.get('parent_comment').strip()
commentNews.save()
subscribed_user = models.SubscribedUsers.objects.create(username=data.get('user_name').strip(),email=data.get('user_email_id').strip())
news = models.News.objects.get(id=int(newsId))
news.comments.add(commentNews)
data ={
'status':'success'
}
else:
data ={
'status':'failure'
}
responseData['result'].append(data)
return Response(responseData,status=status.HTTP_200_OK)
无论何时我在本地检查它都有效,但在服务器端它给我以下错误
ValueError at /service/comment_post/369
No JSON object could be decoded
Request Method: POST
Request URL: http://dev.newskhabari.com/service/comment_post/369
Django Version: 1.9.5
Exception Type: ValueError
Exception Value:
No JSON object could be decoded
Exception Location: /usr/local/lib/python2.7/json/decoder.py in raw_decode, line 383
Python Executable: /var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/bin/python
Python Version: 2.7.6
Python Path:
['/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/site-packages',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/site-packages/django',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/bin',
'/var/www/vhosts/newskhabari.com/newskhabari_dev',
'/usr/local/rvm/gems/ruby-2.2.2/gems/passenger-5.0.30/src/helper-scripts',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python27.zip',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/plat-linux2',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/lib-tk',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/lib-old',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/newskhabari-app-venv/lib/python2.7/site-packages',
'/var/www/vhosts/newskhabari.com/newskhabari_dev',
'/var/www/vhosts/newskhabari.com/newskhabari_dev/app']
Server time: Mon, 17 Oct 2016 11:35:36 +0530
我无法弄清楚它给出的原因 异常值:无法解码JSON对象
答案 0 :(得分:-1)
我猜你正在使用django-rest-framework
。因此,您不必执行json.loads()
,因为django-rest-framework
为request.data
次请求提供POST
,为request.query_params
次请求提供GET
, 已经以json格式解析。
所以我认为这对你有用。
@api_view(['POST'])
def comment_post(request,newsId=None):
responseData= dict({
"result": list()
})
if(newsId):
commentNews = models.Comments.objects.create()
commentNews.comment_description = request.data.get('comment_description').strip()
commentNews.like_count = int(request.data.get('like_count'))
commentNews.user_name = request.data.get('user_name').strip()
commentNews.user_email_id = request.data.get('user_email_id').strip()
commentNews.parent_comment = request.data.get('parent_comment').strip()
commentNews.save()
subscribed_user = models.SubscribedUsers.objects.create(username=request.data.get('user_name').strip(),email=request.data.get('user_email_id').strip())
news = models.News.objects.get(id=int(newsId))
news.comments.add(commentNews)
data ={
'status':'success'
}
else:
data ={
'status':'failure'
}
responseData['result'].append(data)
return Response(responseData,status=status.HTTP_200_OK)
有关详细信息,请阅读docs here