1

I am using Boostrap modal fade window which renders Django form to update my database records. And what I fail to do is not to reload the page if the user has opened the Update window and did not change anything. It will be easier to get my idea if you look at the code below:

def updateTask(request, task_id):
    #cur_usr_sale_point = PersonUnique.objects.filter(employees__employeeuser__auth_user = request.user.id).values_list('agreementemployees__agreement_unique__sale_point_id',flat=True)
    selected_task = Tasks.objects.get(id=task_id)    
    task_form = TaskForm(instance=selected_task )
    taskTable = Tasks.objects.all()
    if request.method == 'POST':
        task_form = TaskForm(request.POST,instance=selected_task)
        if task_form.has_changed():
            if task_form.is_valid():
                # inside your model instance add each field with the wanted value for it
                task_form.save();
                return HttpResponseRedirect('/task_list/')   
        else: # The user did not change any data but I still tell Django to                 
              #reload my page, thus wasting my time.
             return HttpResponseRedirect('/task_list/') 
    return render_to_response('task_management/task_list.html',{'createTask_form':task_form, 'task_id': task_id, 'taskTable': taskTable},context_instance=RequestContext(request))

The question is, is there any way to tell Django to change the url (like it happens after redirecting) but not to load the same page with same data for the second time?

Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
  • 1
    No, I don't think so. Once the `request` is sent to Django, it must return a `response`. You can use some AJAX on the button click to do this. Write an AJAX method that checks if the current form data are different from the saved data. Only if true, submit the full form. – Dan Russell Mar 07 '16 at 16:12
  • @DanRussell, could you please provide even the shortest sample of the structure of this JQuery-Ajax function in an answer to the question? I have very little experience with Ajax, and even though I know the theory, I don't know how this should look like in practice. I would appreciate even a kick towards the right direction – Edgar Navasardyan Mar 08 '16 at 09:31

1 Answers1

2

It's not trivial, but the basic steps you need are:

  1. Write some javascript to usurp the form submit button click
  2. Call your ajax function which sends data to "checking" view
  3. Write a "checking" view that will check if form data has changed
  4. If data have changed, submit the form
  5. If not, just stay on page

This blog post is a nice walkthrough of the entire process (though targeted towards a different end result, you'll need to modify the view).

And here are some SO answers that will help with the steps above:

  1. Basically:

    $('#your-form-id').on('submit', function(event){
        event.preventDefault();
        your_ajax_function();
    });
    
  2. Call ajax function on form submit

  3. Gotta do yourself!
  4. Submit form after checking
Community
  • 1
  • 1
Dan Russell
  • 960
  • 8
  • 22