Search code examples
jqueryajaxdjangodjango-context

django ajax context


How do I use upload_response below in my html/jquery to check if it is true or not, and display some text accordingly without going to a new page?

fileupload.py file:

  template = loader.get_template('start_interview.html')
  context = Context({ 'upload_response': 'True'})
  return HttpResponse(template.render(context))

Solution

  • You will need to use something like jquery for the ajax part.

    $.ajax({
      type: "POST",
      url: "<your url to the view that returns appropriate template>",
      data: { name: "John", location: "Boston" }
    }).done(function( responseMsg ) {
      $('#notifier').html(responseMsg)
    });
    

    The responseMsg will be the rendered template. There is more than one way to do the above.

    In your template

    {%if upload_response %}
     ...Your html tags or jquery if inside script block...
    {%else%}
     ...Do Stuff if it is false...
    {%endif%}