![](/img/trans.png)
[英]how to return an excel sheet and a string response using httpresponse Django Python
[英]How to return both string and value within HttpResponse?
我需要代碼在HttpResponse中返回字符串和值,但它僅返回第一個value(val)。 為什么要同時返回val和val2? 字符串我將用作消息,並且需要在函數return中使用val才能將其用作下一個函數中的值,如下所示:
def func1(request):
data = json.loads(request.body)
val = "string"
val2 = data['email']
return HttpResponse(val, val2)
def func2():
val3 = func1
val4 = "client %s" % val2
if val4:
return True
else:
return False
您的客戶端(網絡瀏覽器)需要一個字符串作為響應:
return HttpResponse("a string: {}".format(val))
或使用JSON作為響應:
return JsonResponse({'message': 'a string', 'val': val})
或者,將變量發送到下一個Django視圖:
def my_view(request):
if request.session.get('val', None):
# do something with the 'val' variable.
else:
request.session['val'] = 'somevalue'
return HttpResponse('some message')
使用JsonResponse
還將解決CORS
問題'''不再為json處理內容類型來添加內容類型。 “””
def __init__(self, content={}, status=None,content_type='application/json'):
super(JsonResponse, self).__init__(
json.dumps(content),
status=status, content_type=content_type)
和響應將是這樣的:
return JsonResponse({"message": " message", "subject": subject})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.