locoboy
locoboy

Reputation: 38960

Checking if authenticated in Django template inheritance

I have the following code in a template and am having difficulty showing when a user is logged in. I will be able to login, but when I revisit the page, it shows that I'm still not authenticated.

  {% extends "base.html" %}

{% load catalog_tags %}
{% block site_wrapper %}
<div id = "main">
  <a href="#content" class="skip_link">Skip to main content</a>
  <div id = "banner">
    <div class="bannerIEPadder">
      <div class="cart_box">
    {% cart_box request %}
      </div>

    </div>
  </div>
  <div style="float:right;">[search box goes here]</div>
  <div id="navigation">
    <div class="navIEPadder">
      <!--navigation tabs at the top of each page -->
      {% comment %}{% include "tags/navigation.html" %} {% endcomment %}
      {% category_list request.path %}
    </div>
  </div>
  <div id="middle">
    <div id="sidebar">
      <!--<div class="sidebarIEPadder">[search box goes here]</br>
    {% comment %}{% category_list request.path %}{% endcomment %}
      </div>-->
    </div>
    <div id="content">
<!--      <a name = "content"></a>-->
      <div class="contentIEPadder">
    {% block content %}{% endblock %}
      </div>
    </div>
  </div>

  <div id="footer">
    <div class="footerIEPadder">
      {% footer_links %}
    </div>
  </div>

</div>
{% endblock %}

And here's the file it references. Since this will be an extension of all templates, is there something I need to consider?

###category_list.html
<!--<h3>Categories</h3>-->

<!--<ul id="categories">-->
<ul>
  {% with active_categories as cats %}
  {% for c in cats %}
  <li> 
    {% comment %}
    {% ifequal c.get_absolute_url request_path %}
    {{c.name}}
    {% else %}
    {% endcomment %}
    <div><a href="{{c.get_absolute_url}}" class="category">{{c.name}}</a></div>
    {% comment %}{% endifequal %}{% endcomment %}
  </li>
  {% endfor %}
  <div class="fr">
    <ul>
      <li>
    {% if user.is_authenticated %}
    <a href = "{% url django.contrib.auth.views.logout %}">Logout</a>
    {% else %}
    <a href = "{% url django.contrib.auth.views.login %}">Login</a>
    {% endif %}
      </li>
  </div>
{% endwith %}


</ul>

<div class="cb"></div>

Am I missing something here?

Upvotes: 1

Views: 1801

Answers (2)

dani herrera
dani herrera

Reputation: 51705

  • Is django.contrib.auth.context_processors.auth in your TEMPLATE_CONTEXT_PROCESSORS setting.py?
  • Do you use requestContext rendering template?
  • Check that sessions are enabled: MIDDLEWARE_CLASSES should contains 'django.contrib.sessions.middleware.SessionMiddleware'

Upvotes: 1

CoderMD666
CoderMD666

Reputation: 1007

You need to pass a RequestContext to the template. The easiest way to do this is to import django.shortcuts and use the render method in your view:

return render(request, "my_template.html")

Upvotes: 3

Related Questions