Django: detect admin login in view or template

Viewed 52456

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

3 Answers

If you don't care about the privileges a user has and just want to make sure they are logged in, you can use user.is_anonymous, which will be true if the user is logged out and false if they are logged in.

By default, user.is_staff is required for the user to have access to the Django Admin.

Related