Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: detect admin login in view or template

How can I detect, from views or template file, if my admin user is logged in or not? For example, use case for my site:

  1. User opens /admin/, enter correct credentials
  2. Then user open some page, for example /page/ and there are "Add" and "Delete" buttons
  3. User opens /admin/ and click "Log out"
  4. /page/ -> there are no "Add" and "Delete" buttons

Django version 1.4

like image 370
Rusty Avatar asked Aug 11 '12 17:08

Rusty


1 Answers

In templates:

{% if user.is_superuser %}     <p>Hello, admin.</p> {% else %}     <p>Hello, ordinary visitor.</p> {% endif %} 

In views:

if request.user.is_superuser:     # Hello, admin. else:     # Hello, ordinary visitor. 

Depending on your needs, is_staff might be a better fit than is_superuser. You can read about the difference here.

like image 143
Joe Mornin Avatar answered Sep 21 '22 05:09

Joe Mornin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!