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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
... then you would do the following within your decorator:
1 2 3 4 5 6 7 8 |
|