0

I am using Django and here are the get and post methods. After refreshing page, on first form submit, Ajax jquery is called. On success, it returns json.msg which is another form rendered as given in below post call. When I try to submit this rendered form, ajax is not called but it directly calls the django POST method.

On ajax success, I tried '$('#school').unbind('submit').submit();'. This way, it ensured to call ajax on every form submit. But, the above method, refreshed the whole page which I do not want. May I know how can I unbind the previous submit without page refresh or get Ajax called(and not direct django post) on every submit.

def get(self, request):
    ...
    ...
    return render(request, self.template_name, {'form':next_form_obj})


def post(self, request):
    ...
    t = get_template('school/fun_activities.html')
    msg = t.render({'form': next_form_obj}, request)
    return JsonResponse({'msg': msg})

Here's the Ajax call.

$('#school').on('submit', function(event){
$.ajax({
    type: 'POST',
    url: '/school/fun/',
    data: $("#school").serialize(),
    success: function (json) {                   
                event.preventDefault();
                $('#msg-list').append(json.msg); #json.msg is another form
    },
        error: function(data) {
        $("#msg-list-div").html("Something went wrong!");
    }
});
return false;
});

HTML:

<form id = "school" method="post" action=".">
    <input type="submit" value="Next" />
</form>
user3665224
  • 700
  • 2
  • 8
  • 24

0 Answers0