title: Passing a session to a Django template via jQuery
body: <p>I'm trying to <code>$.load()</code> a Django template that uses <code>{% if user.is_authenticated %}</code>. Unfortunately, the <code>user</code> object is undefined in the template when rendered on the server <em>only if it's in response to an AJAX-generated HTTP request</em>.</p>
  
  <p>my_template.html:</p>
  
  <pre><code>{% if user.is_authenticated %}
    &lt;div&gt;This should be printed&lt;/div&gt;
  {% else %}
    &lt;div&gt;But this is because there is no user object&lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>my_loader.html:</p>
  
  <pre><code>{% if user.is_authenticated %}
    &lt;div&gt;This works fine because I'm logged in&lt;/div&gt;
  {% endif %}
  &lt;div id="template'&gt;&lt;/div&gt;
  &lt;script&gt;
    $('#template').load('/my_template.html');
  &lt;/script&gt;
  </code></pre>
  
  <p>views.py (using django-annoying's <code>@render_to</code> decorator):</p>
  
  <pre><code>@render_to('my_template.html')
  def my_template(request):
    return {}
  </code></pre>
  
  <p>The problem, as I understand it, is that the "context" available in the rendering of my_loader.html is not made available to the server through <code>.load()</code>. I don't know whether this "context" is a session, a cookie, or something in the header, but the result is that when the server renders my_template.html from an HTTP request generated through AJAX, there is no <code>user</code> object. When I load it organically in the browser it works fine.</p>
  
  <p>If it helps:</p>
  
  <ul>
  <li>This is on the same domain.</li>
  <li>I've already tried using .ajax() instead with <code>xhrFields: { withCredentials: true }</code></li>
  <li>I've already tried using .ajax() instead with <code>headers: { 'sessionid': $.cookie('sessionid') }</code>. The sessionid cookie did not exist (although csrftoken did, so I know I was looking in the right place).</li>
  </ul>
  
  <p>Any idea how to make the <code>user</code> object from my_loader.html available to the server when it loads pages like my_template.html via AJAX?</p>