本篇文章给大家分享的是有关Django中怎么接收post前端返回的json格式数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
post接收字符串
def subscription(request): msg = request.POST.get('msg') # tel_no = request.POST.get('tel_no') # email = request.POST.get('email') # ico_id = request.POST.get('ico_id') data = base64.b64decode(msg) data = data.decode('utf-8') data = json.loads(data) client = pymongo.MongoClient(host = 'localhost',port = 27017) db = client.users my_set = db.user if 'ico_id' not in data.keys(): return HttpResponse(json.dumps({"result_code":1})) result_code = my_set.insert({"tel_no":data.get('tel_no'),"email":data.get('email'),"ico_id":data.get('ico_id'),'date':datetime.datetime.now()}) return HttpResponse(json.dumps({"result_code":0}))
post接收json格式
def selectedico(request): if request.method == 'POST': web_id = json.loads(request.body.decode().replace("'", "\"")).get('id') client = pymongo.MongoClient(host = 'localhost',port = 27017) db = client.webdata my_set = db.webchinadata values = [] #print(web_id) print(request.body.decode()) for val in my_set.find(): # value = value.decode('utf-8') # val = json.loads(value) val["_id"] = str(val["_id"]) val["date"] = str(val["date"]) discount = (''.join(val["discounts"])).split('\n') dis = [x.strip(' ') for x in discount if x != ''] val["discounts"] = dis val["accept_coins"] = val["accept_coins"].split(",") details = (''.join(val["details"])).replace('\n','') val["details"] = details #print(val["_id"]) if val["_id"] == web_id: values.append(val) return HttpResponse(json.dumps(values,ensure_ascii=False),content_type="application/json;charset=utf-8")
以上就是Django中怎么接收post前端返回的json格式数据,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。