Django接收post前端返回的json格式数据代码实现

时间:2021-05-23

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")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章