Skip to content

Decorators

Overview

Decorator functions can be used within your components.py to help simplify development.


Auth Required

You can limit access to a component to users with a specific auth_attribute by using this decorator (with or without parentheses).

By default, this decorator checks if the user is logged in and not deactivated (is_active).

This decorator is commonly used to selectively render a component only if a user is_staff or is_superuser.

1
2
3
4
5
6
7
8
from reactpy import component, html
from reactpy_django.decorators import auth_required


@component
@auth_required
def my_component():
    return html.div("I am logged in!")
See Interface

Parameters

Name Type Description Default
auth_attribute str The value to check within the user object. This is checked via getattr(scope["user"], auth_attribute). "is_active"
fallback ComponentType | VdomDict | None The component or reactpy.html snippet to render if the user is not authenticated. None

Returns

Type Description
#!python Component` A ReactPy component.
#!python VdomDict| A#!python reactpy.html` snippet.
#!python None` No component render.
How do I render a different component if authentication fails?

You can use a component with the fallback argument, as seen below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from reactpy import component, html
from reactpy_django.decorators import auth_required


@component
def my_component_fallback():
    return html.div("I am NOT logged in!")


@component
@auth_required(fallback=my_component_fallback)
def my_component():
    return html.div("I am logged in!")
How do I render a simple reactpy.html snippet if authentication fails?

You can use a reactpy.html snippet with the fallback argument, as seen below.

1
2
3
4
5
6
7
8
from reactpy import component, html
from reactpy_django.decorators import auth_required


@component
@auth_required(fallback=html.div("I am NOT logged in!"))
def my_component():
    return html.div("I am logged in!")
How can I check if a user is_staff?

You can do this by setting auth_attribute="is_staff", as seen blow.

1
2
3
4
5
6
7
8
from reactpy import component, html
from reactpy_django.decorators import auth_required


@component
@auth_required(auth_attribute="is_staff")
def my_component():
    return html.div("I am logged in!")
How can I check for a custom attribute?

You will need to be using a custom user model within your Django instance.

For example, if your user model has the field is_really_cool ...

1
2
3
4
5
6
7
from django.contrib.auth.models import AbstractBaseUser


class CustomUserModel(AbstractBaseUser):
    @property
    def is_really_cool(self):
        return True

... then you would do the following within your decorator:

1
2
3
4
5
6
7
8
from reactpy import component, html
from reactpy_django.decorators import auth_required


@component
@auth_required(auth_attribute="is_really_cool")
def my_component():
    return html.div("I am logged in!")

Last update: September 7, 2023
Authors: Mark Bakhit