Json Django,处理来自外部服务器的响应
问题是如何从外部服务器处理HttpResponce 我的想法是将json数据发送到外部服务器 (例如搜索数据Json Django,处理来自外部服务器的响应,json,django,httprequest,httpresponse,Json,Django,Httprequest,Httpresponse,问题是如何从外部服务器处理HttpResponce 我的想法是将json数据发送到外部服务器 (例如搜索数据{'keyword':关键字,'limit':limit,'db':db}) response=requests.post(url,json=userupload,headers=headers) 之后,我将从服务器获得json数据的响应 返回HttpResponse(响应) 它在屏幕上,但正如您所理解的,用户看不到它 例如,如何将这些数据添加到适当的html表中?(最好的选择是我能在同一
{'keyword':关键字,'limit':limit,'db':db}
)
response=requests.post(url,json=userupload,headers=headers)
之后,我将从服务器获得json数据的响应
返回HttpResponse(响应)
它在屏幕上,但正如您所理解的,用户看不到它
例如,如何将这些数据添加到适当的html表中?(最好的选择是我能在同一页上打印出来)
在django教程中,您将学习如何使用html和上下文数据呈现响应
如果您正在使用,您可以执行以下操作:
response = requests.api.post(...
context = json.loads(response.json())
return render(request, 'index.html', context)
json api的一大优点是,您可以使用javascript异步访问它。如果只想呈现响应而不调用数据库来操作json api中的数据,那么应该注意这一点。如果我理解正确,您希望将post请求的json格式输出呈现到HTML文件中 为此,请将json编码的对象从视图传递到模板: 视图。py:
import json
def myview(request):
obj = requests.post(url, json = userupload, headers=headers)
return render_to_response("template.html", {"obj_as_json": json.dumps(obj.json())})
<html>
<head>
<script type="text/javascript">
var obj = {{ obj_as_json }};
</script>
</head>
...
</html>
template.html:
import json
def myview(request):
obj = requests.post(url, json = userupload, headers=headers)
return render_to_response("template.html", {"obj_as_json": json.dumps(obj.json())})
<html>
<head>
<script type="text/javascript">
var obj = {{ obj_as_json }};
</script>
</head>
...
</html>
var obj={{obj_as_json};
...
如果你不需要“中间人”,为什么不使用javascript?我不太确定你想要实现什么,但这个:可能有用吗?