简体   繁体   English

在Django上对HTTP请求和JSON消息进行故障排除

[英]Troubleshooting HTTP request and JSON message on Django

Okay, so I have been going at this for a while and it doesn't seem like I am getting anywhere. 好吧,所以我已经花了一段时间了,看来我什么也没得到。 I am running a Django app with Nginx and uwsgi. 我正在使用Nginx和uwsgi运行Django应用。 I have an http.post and I am trying to even read the items which I keep getting errors for. 我有一个http.post,我甚至试图阅读我不断出错的项目。

This is what my JS code looks like : 这就是我的JS代码:

$scope.receipt_pay_update = function(items)
{ 

  response = confirm("Do you want to continue with the changes?");
if(!response){

  return;
  }
  var data = {
        'items': items,
        'time_now': moment().format("YYYY-MM-DD")
    };
  items.showmessage = true;
  console.log(data)
       $http.post("/foodhub/dashboard/receipt_pay_modal_update", data,{
           data: JSON
       }).
       success(function(data,status,headers,config){
        $scope.alertclass = 'alert-success';
        $scope.save_message_farmer = "Succcessfully update payment"
        console.log("SAVED!")
       }).
       error(function(data,status,headers,config){
        $scope.alertclass = 'alert-danger';
        $scope.save_message_farmer= "Failed to update inventory, please try again"
       })
}

This is what my views.py looks like: 这是我的views.py的样子:

@login_required

def receipt_pay_modal_update(request):
import sys
reload(sys)
sys.setdefaultencoding('utf8')


data = json.loads(request.body)['items']

print data

rec = ReceiverActions.objets.get(identifier = data[0]['identifier'])

rec['paid_check'] = data[0]['paid_status']
rec['date_paid'] = data[0]['paid_date']

rec.save()
return HttpResponse(status=status.HTTP_200_OK)
  1. I got an error of unable to decode JSON. 我收到无法解码JSON的错误。 So I tried data = request.body[0] which also didn't work. 因此,我尝试了data = request.body[0] ,但该方法也无效。
  2. Is there any other way I could be testing small changes on my server without having to do the Git push, Git Pull, Python -m compileall ., etc? 还有什么其他方法可以在我的服务器上测试小的更改,而无需执行Git push,Git Pull,Python -m compileall等。 The reason I ask is because I was taught to do it this way by practice and I feel there is a better way. 我问的原因是因为实践指导我这样做,所以我觉得有更好的方法。
  3. Where can I check my print data ? 在哪里可以查看我的print data

Any help would be highly appreciated. 任何帮助将不胜感激。

Turns out the data I was getting was not JSON appropriate. 原来我得到的data不适合JSON。 I went back and changed the request to send data as a json and it worked perfectly. 我返回并更改了将数据作为json发送的请求,它运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM