在Django的刷新整个网页中的AJAX?网页、Django、AJAX

2023-09-11 22:29:44 作者:今天假笑了呢

我用ajax去功能并检查值的数据库或不存在。如果数据已经存在,我展示了jQuery的对话框,工作正常,但如果该值不存在,我不希望显示弹出窗口并刷新整个页面。这是我的AJAX功能:

I am using ajax to go the function and check if the value already exist on the database or not. If the data already exist, I show the jQuery dialog which is working fine but if the value doesn't already exist I don't want to show the popup and refresh the entire page. Here's my AJAX function:

function copyFile(val) {
    var choices = document.getElementsByName("choice_shard_with_me").value;
    var file_owner = document.getElementById('username').value;
      $.ajax({
                type: "GET",
                url: "/copy_file/",
                data: {choices:choices, file_owner:file_owner},
                success: function(data){
                     $('#dialog-confirm').html(data)

                }
            });
}

我的Django的观点:

 if request.GET:

    choices = request.GET.getlist('choice_shard_with_me')
    file_owner = request.GET.getlist('username')

    #Test if the file already exist in the user share directory
    x = [File.objects.filter(user_id=request.user.id, file_name=i, flag='A').values_list('file_name') for i in choices]
    y = [y[0] for y in x[0]]
    if len(y) > 1:
        return render_to_response('copyexist.html', {'file':y}, context_instance=RequestContext(request)) 
        //doesn't refresh the whole page show the popup.


    else:
        //refresh whole page and do something

我的问题是:已经弹出,当它在一个div使用Ajax显示。如果它进入复制完成,并成功的消息是在一个小格本身(我想在这里做整页刷新)。

My question is: when the popup is displayed it is shown using Ajax in a div. If it goes into the else statement file copied is done and the successful message is given in a little div itself(I want to do whole page refresh here).

推荐答案

您可以让您的比如JSON EN codeD,在那里你将返回两个参数的回答:

You can make your answer for example json encoded, where you will return two parameters:

 { 
   success: true/false,
   data : YOUR_DATA_HERE
 }

所以,成功的回调可以考虑成功的一部分,并决定是否显示弹出数据,或者使页面重载

So success callback can look into success part, and decide whether to show popup with data, or make page reload

对不起,我不知道蟒蛇,因此我们无法提供确切的EX​​ pression做出正确的JSON恩code。

Sorry, I don't know python, so can't advise exact expression to make correct json encode.