长期,缓慢的操作在Django视图导致超时。任何方式为Python说话的AJAX呢? [英] Long, slow operation in Django view causes timeout. Any way for Python to speak AJAX instead?

查看:471
本文介绍了长期,缓慢的操作在Django视图导致超时。任何方式为Python说话的AJAX呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写Python的一段时间,但Django和Web编程一般是新的我。

I've been programming Python a while, but DJango and web programming in general is new to me.

我有一个Python观点进行了很长的操作。因为在我看来,本地()函数需要如此长的回归,有一个HTTP超时。很公平,我明白那部分。

I have a very long operation performed in a Python view. Since the local() function in my view takes so long to return, there's an HTTP timeout. Fair enough, I understand that part.

什么是立即得到HTT presponse回到我的用户的最佳方式,那么动态显示的页面中的一些蟒蛇code的结果?我怀疑,答案可能在于AJAX,但我;不知道如何AJAX客户端上,可以从Python的喂养在服务器上,或者甚至是一​​个模块将通常使用做这样的事

What's the best way to give an HTTPresponse back to my users immediately, then dynamically show the results of some python code within the page? I suspect the answer may lie in AJAX but I;m not sure how AJAX on the client can be fed from Python on the server, or even the modules one would commonly use to do such a thing.

推荐答案

阿贾克斯不需要在服务器端的任何特定技术。所有你需要的是回到某种形式的一些JavaScript在客户端可以理解的反应。 JSON是一个很好的选择在这里,因为它很容易创建在Python(有一个在2.6 JSON 库和Django的有 django.utils.simplejson 与其他版本的)。

Ajax doesn't require any particular technology on the server side. All you need is to return a response in some form that some Javascript on the client side can understand. JSON is an excellent choice here, as it's easy to create in Python (there's a json library in 2.6, and Django has django.utils.simplejson for other versions).

因此​​,所有你需要做的就是把你的数据在JSON形式然后将其发送就像任何其他的反应 - 即由封装在一个 HTT presponse

So all you need to do is to put your data in JSON form then send it just as you would any other response - ie by wrapping it in an HTTPResponse.

这篇关于长期,缓慢的操作在Django视图导致超时。任何方式为Python说话的AJAX呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆