0

I want to know how do i limit the access of specific users to only one page once they're logged in.

I have in my User model

deactivated = models.BooleanField(default=False)

If the user is logged in and their account is deactivated I want to only show them a deactivation page. I don't want to allow them to go anywhere else on the website unless they activate their account again. What's the best and the most simple way to implement that?

EDIT: I can't afford going through every view I have and attach a decorator to it.

dado_eyad
  • 601
  • 1
  • 8
  • 28

3 Answers3

1

If you don't want to use the decorator approach, your best bet is to write a middleware that checks if request.user is activated or not, then redirect (to a page where they can reactivate their account preferably) when necessary.

Roughly you'd want something like this:

from django.shortcuts import redirect


class DeactivatedRedirectMiddleware(object):

    def process_request(self, request):
        if request.user and not request.user.is_anonymous():
            if request.user.deactivated and request.get_full_path() != '/some/url/':
                # redirect here
                return redirect('/some/url/')
        # ...
dado_eyad
  • 601
  • 1
  • 8
  • 28
K Z
  • 29,661
  • 8
  • 73
  • 78
  • Okay this works I think. but now I'm in a redirect loop. because even going to `/some/url/` redirects me. – dado_eyad Oct 23 '12 at 09:55
  • @dado_eyad you can check if `request.path` is `/some/url/` before redirecting it. – K Z Oct 23 '12 at 10:00
  • 1
    @dado_eyad you are welcome :) Make sure to test more edge cases to be certain edge cases are covered :) – K Z Oct 23 '12 at 10:02
0

You can use decorator function to check if user is activated and redirect him. How to write a custom decorator in django?

Community
  • 1
  • 1
szaman
  • 6,666
  • 13
  • 53
  • 81
0

Use a view decorator.

Good article about it: http://passingcuriosity.com/2009/writing-view-decorators-for-django/

Ponytech
  • 1,634
  • 14
  • 21