【问题标题】:Reload entire page in Ajax on Django?在 Django 上的 Ajax 中重新加载整个页面?
【发布时间】:2013-07-02 06:56:49
【问题描述】:

我正在使用 ajax 来执行该函数并检查该值是否已存在于数据库中。如果数据已经存在,我会显示工作正常的 jQuery 对话框,但如果该值不存在,我不想显示弹出窗口并刷新整个页面。这是我的 AJAX 函数:

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 显示。如果它进入复制完成的 else 语句文件,并且成功的消息会在一个小 div 本身中给出(我想在这里刷新整个页面)。

【问题讨论】:

  • 做一个 len(y) 的 console.log 看看你得到了什么!
  • 它是动态的。如果 len(y) 小于 0,它将转到 else。如果 len(y) > 1 (这意味着有一个文件)所以应该显示弹出窗口。
  • 写入 console.log 以查看 y = [y[0] for y in x[0]] 这一行之后的 len(y) 的值。我的猜测是它在 len(y) 中得到了不正确的值。尝试探查这个 len(y) 的值
  • 好吧,如果你确定这个值,你可以写 location.reload();在你的其他部分
  • 我的视图在我的服务器中。 location.reload 将如何在服务器上工作?

标签: ajax django jquery


【解决方案1】:

你可以让你的答案例如 json 编码,你将返回两个参数:

 { 
   success: true/false,
   data : YOUR_DATA_HERE
 }

所以成功回调可以查看成功部分,并决定是显示弹出数据,还是重新加载页面

对不起,我不懂python,所以不能建议准确的表达方式来进行正确的json编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2014-09-06
    • 2012-08-25
    • 1970-01-01
    • 2011-09-12
    • 2023-04-11
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多