如何创建Django的一个等待页面 [英] How to create a waiting page in Django
问题描述
我构建需要一个漫长的计算应用程序。后一个用户提交的信息,它需要约30分钟来计算,然后返回结果。所以我正在考虑添加一个请稍候的页面。
我也跟着在下面的链接中提到的指令,
http://groups.google.com/group/django-users/browse_thread /线程/ c1b0d916bbf86868
然而,当我提出的东西,它停留在 http://127.0.0.1:8000/please_wait
并不会重定向到像 http://127.0.0.1:8000/display_DHM
有谁知道这是怎么回事?
下面是所有相关的文件,我想尽各种办法,但是当我 提交一个表单,它仅返回PLEASE_WAIT页面,然后呆在那里 永远。没有重定向发生。 因为我要检查,如果它的工作原理首先,没有实际 计算在code。
url.py
URL模式=模式('',
(R'^测试$',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
高清test_form(要求):
返回render_to_response('的test.html')
高清PLEASE_WAIT(要求):
返回render_to_response('please_wait.html)
高清run_DHM(要求):
###冗长的计算......
返回的Htt presponse(OK)
高清display_DHM(要求):
返回render_to_response('display_DHM.html)
的test.html
{%伸出baseFrame.html%}
{%块日程地址搜索Maincontent%}
<形式方法=POST行动=PLEASE_WAIT>
&其中p为H.;试验:其中; / P>
< DIV ID =地址>< / DIV>
< P>在此输入您的值:其中; / P>
< P>< textarea的名字=订单行=6COLS =50ID =订单>< /
textarea的>< / P>
< P><输入类型=提交值=提交ID =提交/>< / P>
< /形式GT;
{%端嵌段%}
please_wait.html
< HTML>请稍候
<脚本类型=文/ JavaScript的SRC =HTTP://$c$c.jquery.com/
jQuery的-1.7.1.min.js>
$ .getJSON('{%URL run_DHM%}',功能(数据){
如果(数据=='OK'){
window.location.href ='{%URL displayDHM%};
} 其他 {
警报(数据);
}
});
< / SCRIPT>
< / HTML>
display_DHM.html
< HTML>
< BODY> end最后<!/ BODY>
< / HTML>
我写在这里,因为我不能用注释的空间。我的<一个href="http://stackoverflow.com/questions/28837217/django-create-a-changing-button-or-waiting-page">question有点类似于你的,也许回答可以帮助你。
简而言之:
问题:
我有一个外部python程序,名为c.py,其罪状长达20秒。我从我的Django应用views.py调用它,并在HTML页面中我有一个按钮来启动它。这是确定(=在Eclipse中我可以看到c.py打印0,1,2,3,... 20的时候我preSS网页上的按钮),但我想从按钮变为GO在c.py过程中等待(或我想计数或还弹出过程中执行一个等待页面)。
答案:
您需要能够给客户端C的情况汇报 通过AJAX长轮询或WebSockets的,或者,如果你不关心 的C递增状态,只是想改变链接的文字, 你需要使用JavaScript来设置值时的单击事件 链接火灾:
views.py
从django.core.urlresolvers导入反向 从django.http进口JsonResponse 高清CONTA(要求): c.prova(0) 重定向=反向('name_of_home_user_view) 返回JsonResponse({重定向:重定向})
和 JS
//假设的jQuery的简洁... $(文件)。就绪(函数(){ //避免硬编码的网址... VAR yourApp = { contaUrl:{%URL'CONTA%} }; $('#btnGo)。点击(函数(五){ 即preventDefault(); // prevent从导航链接 //设置CSS类和按钮的文字 $(本) .removeClass(BTN-小学) .addClass(BTN-危险) 的.text(等待); $获得(yourApp.contaUrl,函数(JSON){ window.top = json.redirect; }); }); });
I am constructing an application that need a lengthy calculation. After a user submitted the information, it need about 30 minutes to calculate and then return the result. So I am considering to add a "please wait" page.
I followed instructions mentioned in the following link,
http://groups.google.com/group/django-users/browse_thread/thread/c1b0d916bbf86868
However, when I submit something, it stays in http://127.0.0.1:8000/please_wait
and will not redirect to the result page like http://127.0.0.1:8000/display_DHM
does anybody know what is going on?
Here are all related files, I tried various ways, but when I submit a form, it only return the please_wait page and then stay there forever. There is no redirect happened. Since I want to check if it works first, there is no actual calculation in the code.
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')
test.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>
I write here because I can't use the comment space. My question is a bit similar to yours and maybe the answer can help you.
Briefly:
the 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).
the answer:
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})
and 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; }); }); });
这篇关于如何创建Django的一个等待页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!