Best practice to render the view based on the user

16 views
Skip to first unread message

Karim

unread,
Jan 28, 2015, 12:04:06 AM1/28/15
to Django Users
Hi! I would like to render views based on the user. I have some differents type of users on my app and I would like to create a class like this one for the view:

class MyView(MyCustomView)  # MyCustomView subclass View 

    def __init__(self, *args, **kwargs):
        # common code for all persons

    def personA(self):
        # code for personA

    def personB(self):
        # code for personA

    # ....

    def personN(self):
        # code for personA

    def anyPerson(self)
        # For any user non listed above
    

Then if the MyView is run, the function based on the user is automatically fired.

My doubts (at the moment):

1) Do you think is the a good way to face the problem?

2) I have methods in the models to know what person is logged. How I can automatically load the right function in the class based on the user?

Thank you

--
Karim N. Gorjux

Collin Anderson

unread,
Jan 30, 2015, 8:34:15 AM1/30/15
to django...@googlegroups.com
Hi,

Not sure about whether it's a good idea or not. If statements might also be good enough.

Be careful with security, but in theory something like this should work:

method_name = 'person' + person.type
method = getattr(self, method_name)
output = method()

Collin
Reply all
Reply to author
Forward
0 new messages