Django:创建一个“更改按钮”或等待页面 [英] Django: create a "changing button" or waiting page

查看:535
本文介绍了Django:创建一个“更改按钮”或等待页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部python程序,命名为 c.py ,计数最多20秒。
我从Django应用程序 views.py 中调用它,在html页面中我有一个按钮启动它。没关系(=在Eclipse中我可以看到,c.py打印0,1,2,3,... 20当我按下网页上的按钮),但我希望按钮从GO改为等待 在 c.py 过程中(或者我想在计数期间执行一个等待页面,或者也是一个弹出窗口)。

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).

c.py

import time

def prova(z):
    z = 0
    while z < 20:
        time.sleep(1)
        z = z + 1
        print(z)

views.py

    from django.shortcuts import render_to_response
    #etc.

    import c


def home_user(request):
    return render_to_response('homepage/home.html',{'user.username':request}, context_instance = RequestContext(request))

def conta(request):
    c.prova(0)
    return redirect(home_user)

在homepage.html中我有GO按钮,我想在等待中更改,如果功能 conta 正在运行。

where in homepage.html I have the "GO" button that I would like to change in "WAIT" if the function conta is running.

urls.py

        urlpatterns =patterns('',
    url(r'^homepage/home/$', views.home_user, name='home'),
    #etc.
    url(r'^conta', views.conta, name='conta'),


)

home.html

{% if c.alive %}
<a href="" class="btn btn-danger" role="button">WAIT</a>
{% else %}
<a href="/conta/" class="btn btn-primary" role="button">GO</a>
{% endif %}

我不放整个代码..我希望这足以理解我的麻烦。
我也看到在如何在Django中创建一个等待的页面但是我会从一些简单的开始。

I don't put the whole code.. I hope this is sufficient to understand my trouble. I also see at How to create a waiting page in Django but I would start with something simpler.

到目前为止,当我开始使用c.py 时,我看到我的网页是加载某些东西(=这是计数),但是我的按钮不会改变,而 c.py执行后,我返回到 127.0.0.1:8000/homepage/home / 页面。
HTML或我的函数定义或两者都是问题?

Up to now when I start c.py I see that my web page is loading something (=it is "counting") but my button does not change and, after c.py execution, I return to 127.0.0.1:8000/homepage/home/ page. Is the problem in html or in my function definition or both?

更新

我尝试简化问题:
我发现这个脚本...

I try to simplify the question: I found this script...

    <button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var text = "";
    var i = 0;
    while (i < 10) {
        text += "<br>The number is " + i;
        i++;
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

我想在while中导入我的 conta()函数而不是使用i ++的cicle

I would like to "import" my conta() function in while instead of the cicle with i++

ie我想要一个类似的东西:
while conta()正在运行,出现像

等待..

i.e. I would like to have a similar thing: while conta() is running, appear something like

Waiting..

推荐答案

您正在尝试检查客户端上的服务器端值,但问题是如果c.alive 语句仅在渲染视图时被评估 - 而不是 c 更改的状态。

You're trying to check a server-side value on the client, but the problem is that the if c.alive statement only gets evaluated when your view is rendered - not as the status of c changes.

您需要能够通过ajax长轮询或WebSockets向客户端报告 c 的状态,或者您不关心 c 的增量状态,只是想更改链接的文本,您需要使用JavaScript来设置点击事件时的值的链接触发:

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:

// 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;
        });
    });
});

但...您的 conta 函数将需要返回 JsonResponse 而不是 HttpResponse ,以便在客户端执行重定向:

but... your conta function will need to return a JsonResponse instead of an HttpResponse in order to do the redirect on the client-side:

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})

这篇关于Django:创建一个“更改按钮”或等待页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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