dado_eyad
dado_eyad

Reputation: 601

limit some users to only one page in Django

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.

Upvotes: 0

Views: 423

Answers (3)

K Z
K Z

Reputation: 30453

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/')
        # ...

Upvotes: 1

Ponytech
Ponytech

Reputation: 1684

Use a view decorator.

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

Upvotes: 0

szaman
szaman

Reputation: 6756

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

Upvotes: 0

Related Questions