title: How to fire an 'if condition' after making an ajax call to a view?
body: <p>I'm building a search bar that searches for objects in my database and if the search finds some matching objects, it then displays the objects in the html page without reloading it. But after I fire the Ajax call and get the tuples from the database, they are not being rendered on the template</p>
  
  <p>So, the flow is like this: I search for 'apple' in the search bar, hit search button and fire the ajax call to my view. After that, I set a boolean value 'search_successful' to 'True' if I find any 'apple' in my database. I then pass it to my template from where I fired the ajax in the first place. Now I am successfully getting the data and the boolean sets to 'True' correctly. Until this point everything works perfect. But after passing the context to my template,  It does not fire the <strong>if condition</strong> that will display the data if <strong>search_successful = True</strong>. All this is carried out by an ajax call without reloading the page.</p>
  
  <p>Here is the <strong>if condition</strong> which I expect to work after the ajax call has been made to print the data:</p>
  
  <pre><code>&lt;form  id="search_object"&gt;
      {% csrf_token %}
      &lt;input type="text" id="search_my_object" placeholder="Search Item" id="search_my_object" class="form-control" required="true" style="border-color:#f4511e;"/&gt;
      &lt;br&gt;
      &lt;input type="submit" value="SEARCH" class="btn btn-lg" style="border-color:#f4511e; background-color:Transparent; color:white; margin-left:47%;"&gt;
  &lt;/form&gt;
  
  {% if search_successful is True %}
      # Just print the retrieved data passed in the context
  {% endif %}
  
  &lt;script type="text/javascript"&gt;
      $(document).on('submit', '#search_object', function(e){
          e.preventDefault();
          $.ajax({
              type: 'POST',
              url: '{% url 'daily_object_search' %}',
              data: {
                 search_object_name: $('#search_my_object').val(),
                 csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
              },
              success: function(data){
                  if(data == "true"){
                      alert('This Object is Present');
                  }
                  else if(data == "invalid_query"){
                      alert("Please enter a valid object to be searched");
                  }
                  else{
                      alert('This object is not present');
                  }
              }
  
          });
      });
  </code></pre>
  
  <p>Views.py :</p>
  
  <pre><code>def daily_object_search(request):
      context = {'search_successful': False}
      if request.method == 'POST':
          object_name = request.POST['search_object_name']
          print("The query for search :", object_name)
          if len(object_name) &lt;= 2:
              print("The length of the object name :", len(object_name))
              return HttpResponse('invalid_query')
          my_cursor = mydb.cursor()
          sql = "select * from education_fruits where tag = '" + object_name + "'"
          my_cursor.execute(sql)
          result = my_cursor.fetchall()
          print(result)
          if my_cursor.rowcount &lt;= 0:
              print("No object found")
              return HttpResponse('false')
          context['search_successful'] = True
          # Just append the 'result' data in the context before sending it
      return HttpResponse('true', context)
  </code></pre>
  
  <p>I tried printing the data in the if condition but removed it as it was giving me the mentioned error in the browser console : <strong>XML Parsing Error: syntax error Location: <a href="http://127.0.0.1:8000/login/daily_object_search" rel="nofollow noreferrer">http://127.0.0.1:8000/login/daily_object_search</a>
  Line Number 1, Column 1</strong>. 
  Am I missing something in my understanding of ajax calls and templates?</p>