[英]How to return both string and value within HttpResponse?
I need the code to return a string and value in HttpResponse but it returns only first value(val). 我需要代码在HttpResponse中返回字符串和值,但它仅返回第一个value(val)。 Why should I do to return both val and val2? 为什么要同时返回val和val2? String I will use as message and I need val in function return to use it as value in next function,something like this: 字符串我将用作消息,并且需要在函数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
your client (web browser) expects one string as response: 您的客户端(网络浏览器)需要一个字符串作为响应:
return HttpResponse("a string: {}".format(val))
or use JSON for the response: 或使用JSON作为响应:
return JsonResponse({'message': 'a string', 'val': val})
or, to send a variable to the next Django view: 或者,将变量发送到下一个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')
Use JsonResponse
That will solved CORS
problem also ''' Handling content type for json no more to add content-type. 使用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)
and response will be like this : 和响应将是这样的:
return JsonResponse({"message": " message", "subject": subject})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.