0

So problem is following

I have app with few pages. On the top I have menu with standard login/logout and form to login . This menu is in my base.html is suppose to show you the right choices in case you are already logged in /or not yet logged in. All pages in the app are protected with @login_required

base.html

<ul class ="nav navbar-nav navbar-right">
  {% if request.user.is_authenticated %}
      <li ><a href="{% url 'auth_logout' %}" %>  {{user}} Logout  </a></li>
  {% else %}
  <li ><a href="{% url 'registration_register' %}" %> Register </a></li>
       {% endif %}
  <li><div class="form">
        {% if not request.user.is_authenticated and not "/accounts/login" in request.get_full_path %}

          <form class='navbar-form navbar-right' method='POST' action='{% url "auth_login" %}'>{% csrf_token %}
              <div class='form-group'>
                  <input type='text' class='form-control' name='username' placeholder='Username' /> 
              </div>
              <div class='form-group'>
                  <input type='password' class='form-control' name='password' placeholder='Password' />
              </div>
              <button type='submit' class='btn btn-default'>Login</button>
          </form>
        {% endif %}
  </li>
</ul>

So mainly it works perfect with only exception when I call my list pages. And in this case it does pass the @login_required validation but the user object is not visible in the template - for this reason it display the wrong menu options

view.py

    @login_required
    def contact_list(request):
        contact_list = ContactFilter(request.GET, queryset=Contacts.objects.all())
        #return render_to_response('my_app/template.html', {'filter': filter})
        #company_list = Company.objects.all()
        paginator = Paginator(contact_list, 25) # Show 25 contacts per page

        page = request.GET.get('page')
        try:
            contact = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            contact = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            contact = paginator.page(paginator.num_pages)

        #return render(request, 'customer/company_list.html', {'company': company})
        return render_to_response('customer/contact_list.html', {'contact': contact_list,'page_contact':contact })

I don't think contact_list template has anything to do with it so i am not publishing the template code.

Any ideas what can be the reason?

Thank you

Ilya Bibik
  • 3,924
  • 4
  • 23
  • 48

1 Answers1

4

Please use render instead of render_to_response. You are not sending request object to the template, therefore 'request.user.is_authenticated' will not work.

anonDuck
  • 1,337
  • 1
  • 9
  • 15