从Django views.py返回HttpResponse对象和呈现页面
我是Django的新手,一直在通过修改一些现有代码来学习。在views.py文件中的原始代码有一个方法,该方法通过单击按钮将HTTP响应对象(我们称之为从Django views.py返回HttpResponse对象和呈现页面,django,Django,我是Django的新手,一直在通过修改一些现有代码来学习。在views.py文件中的原始代码有一个方法,该方法通过单击按钮将HTTP响应对象(我们称之为resp)返回到浏览器。 我希望能够 单击该按钮打开一个新页面(我正在使用render()函数执行此操作) 以及 将resp传递给它(这是因为我使用的第三方API需要此HttpResponse对象才能工作) 还有什么我可以做的吗?我想将resp作为render()函数中的context参数的一部分传递,但我不明白如何从该context字典中收集值
resp
)返回到浏览器。
我希望能够
resp
传递给它(这是因为我使用的第三方API需要此HttpResponse
对象才能工作)李>
还有什么我可以做的吗?我想将resp
作为render()
函数中的context
参数的一部分传递,但我不明白如何从该context
字典中收集值,然后将其返回到浏览器
编辑:这是views.py文件中的代码:
def call(request):
"""Returns TwiML instructions to Twilio's POST requests"""
response = Dial(caller_id=settings.TWILIO_NUMBER)
# If the browser sent a phoneNumber param, we know this request
# is a support agent trying to call a customer's phone
if 'phoneNumber' in request.POST:
response.number(request.POST['phoneNumber'])
else:
# Otherwise we assume this request is a customer trying
# to contact support from the home page
response.client('support_agent')
response = str(response)
probe = response.find(">")
response = response[:probe+1] + "<Response>" + response[probe+1:] + "</Response>"
print('response:', response)
context = {'response': HttpResponse(response)}
return render(request, 'browser_calls/call_in_progress.html', context) # originally HttpResponse(response) was being returned here because that was required for the call to be placed
def调用(请求):
“”“将TwiML指令返回到Twilio的POST请求”“”
响应=拨号(呼叫者id=设置。电话号码)
#如果浏览器发送了一个phoneNumber参数,我们知道这个请求
#支持代理是否正在尝试拨打客户的电话
如果request.POST中有“phoneNumber”:
响应编号(请求编号['phoneNumber'])
其他:
#否则,我们假定此请求是正在尝试的客户
#从主页联系技术支持
响应。客户(“支持\代理”)
响应=str(响应)
探测=响应。查找(“>”)
响应=响应[:探测+1]+“”+response[探测+1:::+“”
打印('响应:',响应)
上下文={'response':HttpResponse(response)}
return render(请求'browser_calls/call_in_progress.html',context)#最初在此处返回HttpResponse(response),因为这是放置调用所必需的
您可以使用locals()从该范围返回所有变量
点击该按钮打开一个新页面
在前端执行,不应该在后端执行。第三方API不可能需要Django HttpResponse,因此这里显然有些东西被混淆了。您应该显示调用API的代码。那么,在这段代码中,哪里有需要Django HttpResponse的第三方API?@BearBrown onclick功能在js文件中指定。在Django中,我认为views.py与浏览器进行通信,如下所示:@DanielRoseman拨号功能是他们实现的,他们需要我将HTTP响应对象返回到浏览器。我认为拨号对象(作为响应传递)的情况是,它被用来最终进行调用。这是如何回答问题的?如果问题是将响应传递给前端,将locals()作为上下文可能会解决它!我是否还需要将resp
对象附加到此处的某个位置?
context = locals()
return render(request, template, context)