0

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)

                }
            });
}

My Django view:

 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

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

pynovice
  • 7,424
  • 25
  • 69
  • 109
  • do a console.log of len(y) and see what you get! – beNerd Jul 02 '13 at 06:59
  • It's dynamic. If len(y) is less than 0 it will go to else. If len(y) > 1 (it mean's there's a file) so should show the popup. – pynovice Jul 02 '13 at 07:01
  • 1
    write console.log to see the value of len(y) after this line y = [y[0] for y in x[0]]. My guess is that it gets a incorrect value in len(y). Try probing in for the value of this len(y) – beNerd Jul 02 '13 at 07:04
  • 1
    well if you are sure of the value, you can just write location.reload(); in your else part – beNerd Jul 02 '13 at 07:10
  • My View is in my server. How location.reload will work on the server? – pynovice Jul 02 '13 at 07:12
  • please tag your server side technology in the tags so that we know more about what exactly are you using on server side. – beNerd Jul 02 '13 at 07:15
  • http://stackoverflow.com/questions/3553955/refresh-template-in-django – beNerd Jul 02 '13 at 07:25
  • I can see several flaws in your view function. It does not seem to be serving an ajax request first of all. – Ankit Jaiswal Jul 02 '13 at 07:26

1 Answers1

1

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

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

Vadym Kovalenko
  • 652
  • 1
  • 5
  • 14