Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for authenticated user in Pyramid templates?

I am using Pyramid 1.3b with Mako templating. I have a base template that displays some navigational components based on whether the user is logged-in or not. I am doing it this way:

      <%!
        from pyramid.security import authenticated_userid

        def is_authenticated(request):
          return authenticated_userid(request)
      %>

% if is_authenticated(request):
        <!-- 2 Column -->
        <div class="row-fluid main-content">
          <div class="span2">
            <ul class="nav nav-list">
              <li class="nav-header">
                Company A
              </li>
              <li class="active"><a herf="/product/add">Product</a></li>
              <li><a href="#">Order</a></li>
            </ul>
          </div>
          <div class="span10">
            ${self.body()}
          </div>
        </div>
        <!-- End of 2 column -->
% else:
        ${self.body()}
% endif

So, I am trying to display a two column layout if the user is logged-in, and just a 100% width div if the user is not logged in.

I am using a block of code defined on top to check if the the user is authenticated. I'm wondering if there's a better way to do this in Pyramid?

Regards, Mark Huang

like image 507
Mark Avatar asked Apr 02 '12 10:04

Mark


1 Answers

This is fine. Normally in the interest of separating presentation and logic, you would pass into your template the fact that a user is logged in, instead of computing it in mako. Also usually you need more information about the user than just that they are logged in. For that, the following cookbook recipe is quite useful.

http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/auth/user_object.html

like image 140
Michael Merickel Avatar answered Nov 12 '22 11:11

Michael Merickel