title: Django context variables and ajax response
body: <p>I'm working on a basic search for my blog, and the basic functionality is up and working. What I'm wondering, however, is if you can return context variables in an ajax response, which could then be accessed in the template. Here's my search def: </p>
  
  <pre><code>from django.http import HttpResponse
  from django.db.models import Q
  from django.core import serializers
  
  def search(request):
      if request.is_ajax():
          query = request.POST['query']
          text_list = Text.objects.filter(Q(title__icontains=query) | Q(mytextfield__icontains=query))
          data = serializers.serialize("json", text_list)
      else:
          data = "whoops"
      return HttpResponse(data,'application/javascript')
  </code></pre>
  
  <p>This is requested through jquery. </p>
  
  <pre><code>$(document).ready(function() {
      $('#search').submit(function(){
          $.post("/search/search/", $("#search").serialize(), function(data){
              // Search results for: **query**
              $.each(data, function(index){
                  $(ajax).append(data[index].fields.title + "&lt;br&gt;");
              }); 
          }, "json"); 
          return false
      });
  });
  </code></pre>
  
  <p>What I'm wanting to do is pass the <em>query</em> variable, contained in def search, back to my jquery function. In a regular http response, you'd use context variables... but I'm not sure how those could be passed with jQuery. </p>