gpt4 book ai didi

ajax - 如何在 Django 中创建等待页面

转载 作者:行者123 更新时间:2023-12-01 05:02:48 25 4
gpt4 key购买 nike

我正在构建一个需要长时间计算的应用程序。用户提交信息后,大约需要30分钟的时间来计算并返回结果。所以我正在考虑添加一个“请稍候”页面。

我按照以下链接中提到的说明进行操作, http://groups.google.com/group/django-users/browse_thread/thread/c1b0d916bbf86868但是,当我提交某些内容时,它会保留在 http://127.0.0.1:8000/please_wait并且不会重定向到像 http://127.0.0.1:8000/display_DHM

这样的结果页面

有谁知道这是怎么回事吗?

这里是所有相关文件,我尝试了各种方法,但是当我提交表单,它只返回 please_wait 页面,然后停留在那里永远。没有发生重定向。因为我想先检查它是否有效,所以没有实际的代码中的计算。

url.py

urlpatterns = patterns('',
(r'^test$',views.test_form),
(r'^please_wait', views.please_wait),
url(r'^run_DHM$', views.run_DHM, name="run_DHM") ,
url(r'^displayDHM', views.display_DHM, name="displayDHM")
)

View .py

def test_form(request):
return render_to_response('test.html')

def please_wait(request):
return render_to_response('please_wait.html')

def run_DHM(request):
### lengthy calculations... ...
return HttpResponse("OK")

def display_DHM(request):
return render_to_response('display_DHM.html')

测试.html

{% extends "baseFrame.html" %}

{% block maincontent %}
<form method="POST" action="please_wait">
<p>Test:</p>
<div id="address"></div>
<p>Type your value in here:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></
textarea></p>
<p><input type="submit" value="submit" id="submit" /></p>
</form>
{% endblock %}

please_wait.html

<html>Please wait
<script type="text/javascript" src="http://code.jquery.com/
jquery-1.7.1.min.js">
$.getJSON('{% url run_DHM %}', function(data) {
if (data == 'OK') {
window.location.href = '{% url displayDHM %}';
} else {
alert(data);
}
});
</script>
</html>

display_DHM.html

<HTML>
<BODY>END FINALLY!</BODY>
</HTML>

最佳答案

我写在这里是因为我无法使用评论空间。我的question和你的有点相似,也许答案可以帮助你。

简单地说:

问题:

I have an external python program, named c.py, which "counts" up to 20 seconds. I call it from my Django app views.py and in the html page I have a button to start it. It's ok (= in Eclipse I can see that c.py prints 0,1,2,3,...20 when I press the button on the webpage) but I would like that the button changes from "GO" to "WAIT" during c.py process (or I would like to perform a waiting page during the counting or also a pop-up).

答案:

You would need to be able to report back the status of c to the client via ajax long polling or WebSockets, or, if you don't care about the incremental status of c and just want to change the text of the link, you'll need to use JavaScript to set the value when the click event of the link fires:

views.py

from django.core.urlresolvers import reverse
from django.http import JsonResponse

def conta(request):
c.prova(0)
redirect = reverse('name_of_home_user_view')
return JsonResponse({'redirect': redirect})

js:

// assuming jQuery for brevity...

$(document).ready(function() {

// avoid hard-coding urls...
var yourApp = {
contaUrl: "{% url 'conta' %}"
};

$('#btnGo').click(function(e) {
e.preventDefault(); // prevent the link from navigating

// set css classes and text of button
$(this)
.removeClass('btn-primary')
.addClass('btn-danger')
.text('WAIT');

$.get(yourApp.contaUrl, function(json) {
window.top = json.redirect;
});
});
});

关于ajax - 如何在 Django 中创建等待页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com