Django没有文字响应 [英] Django no text in response
问题描述
在我的views.py的某处,我有
def loadFcs(request):
r = requests.get ('a url')
res = json.loads(r.text)
#Do其他东西
返回HttpResponse('some response')
现在,当我从我的javascript中调用它时,loadFcs被调用,并且可能 request.get被异步调用。所以我最终看到'typeError at / loadFcs预期的字符串或缓冲区',跟踪指向
res = json .loads(r.text)
我还修改了我的代码来检查问题,
$ b $ load(r.text)
如果r == None:
print'r is none'
如果r.text == None:
print'text is None'
#Do其他东西
返回HttpResponse('some response')
并注意到'text is none'。所以我想我需要调整代码,以便request.get是同步的。我认为方法执行继续,并且即使在r.text有一些值之前也会返回return语句。
建议?
好的,所以我用python命令行尝试了同样的事情,但它没有相同的代码在我的服务器。
那么问题是什么?
显然,response.text是一些编码(UTF8),我的服务器没有设置为接收,所以只是扔掉它,因此为空。
解决方案:使用response.content(这是原始二进制文件)
somewhere in my views.py,I have
def loadFcs(request):
r = requests.get('a url')
res = json.loads(r.text)
#Do other stuff
return HttpResponse('some response')
Now when I call this from my javascript, loadFcs gets called, and probably requests.get gets called asynchronously. So I end up seeing ' TypeError at /loadFcs expected string or buffer' and the trace points to the line with
res = json.loads(r.text)
I also modified my code to check whats the problem, and
def loadFcs(request):
r = requests.get('a url')
res = json.loads(r.text)
if r == None:
print 'r is none'
if r.text == None:
print 'text is None'
#Do other stuff
return HttpResponse('some response')
and noticed that 'text is none'. So I think I need to adjust code so that request.get is synchronous. I think the method execution continues and the return statement is hit even before r.text has some value. Suggestions?
okay, so I tried the same thing with python command line and it worked BUT not with the same code in my server. So what was the problem?
apparently, response.text is in some encoding ( UTF8) which my server was not set to receive, so it was just throwing it away and hence null.
Solution: use response.content ( which is raw binary)
这篇关于Django没有文字响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!