在服务器运行时显示消息查询Django

时间:2011-10-31 06:19:00

标签: html django

我有一个很长的任务,一旦客户端/用户/客户按下提交按钮就会运行。如何显示消息说,挂起...查询运行,直到查询完成使用Django?

2 个答案:

答案 0 :(得分:1)

你应该考虑使用Ajax来做到这一点。

让我们看一个使用JQuery的例子; 如果在您的模板中,您有以下表单:

<form id="my_form" action="" method="post">
   <label for="age">Age:</label>
   <input type="text" name="age" id="age"/>
   <input type="submit" value="Submit"/>
</form>
<div id="loading" style="display:none;">Loading...</div>

假设您已经包含了Jquery库,我们编写一些Javascript:

$(function(){
    $('#my_form').submit(function(event){
       event.preventDefault();

       alert('user submitted the form');
       $('#loading').show();

       $.post('my_url',
              {age : $('#age').val()},
              function(data){
                  $('#loading').hide();
                  alert('server finished to process data.');
              });
    });

});

您可以完全自定义此代码以执行您想要的操作。 如果你想在ajax中进行更多的调试,我建议你按照这种方式声明你的ajaxSetup:

function ajaxSetup(){

    $.ajaxSetup({
        error:function(x,e){
            if(x.status==0){
                alert('You are offline!!\n Please Check Your Network.');
            }else if(x.status==404){
                alert('Requested URL not found.');
            }else if(x.status==500){
                alert('Internal Server Error.\n' + x.responseText);
            }else if(e=='parsererror'){
                alert('Error.\nParsing JSON Request failed.');
            }else if(e=='timeout'){
                alert('Request Time out.');
            }else {
                alert('Unknow Error.\n'+x.responseText);
            }
        }
    });
}

答案 1 :(得分:0)

这是我最终使用JQuery并包含一些代码来确定按下了哪个按钮:

<form>
<div id="loading" style="display:none;">Finding all words, may take a long time</div>
<div id="form_input_things">
<input type="submit" name="_save" value="Save" />
<input type="submit" name="_get_words" value="Get Words" />
{{ form.as_p }}
</div>
</form>

<script>
    $("form").submit(function() {
    {
      if( $("[submit_button_pressed=Save]").get(0) === undefined )
      { 
        $('#loading').show();
        $('#form_input_things').hide();
      }
    }
    return true;    
    });

    $(":submit").live('click', function() {
       $(this).attr("submit_button_pressed", $(this).val());
    });
</script>