Actions
starlette_admin.actions
action(name, text, confirmation=None, submit_btn_class='btn-primary', submit_btn_text=_('Yes, Proceed'), icon_class=None, form=None, custom_response=False)
Decorator to add custom batch actions to your ModelView
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
unique action name for your ModelView |
required |
text
|
str
|
Action text |
required |
confirmation
|
Optional[str]
|
Confirmation text. If not provided, action will be executed unconditionally. |
None
|
submit_btn_text
|
Optional[str]
|
Submit button text |
lazy_gettext('Yes, Proceed')
|
submit_btn_class
|
Optional[str]
|
Submit button variant (ex. |
'btn-primary'
|
icon_class
|
Optional[str]
|
Icon class (ex. |
None
|
form
|
Optional[str]
|
Custom form to collect data from user |
None
|
custom_response
|
Optional[bool]
|
Set to True when you want to return a custom Starlette response from your action instead of a string. |
False
|
Usage
class ArticleView(ModelView):
actions = ['make_published', 'redirect']
@action(
name="make_published",
text="Mark selected articles as published",
confirmation="Are you sure you want to mark selected articles as published ?",
submit_btn_text="Yes, proceed",
submit_btn_class="btn-success",
form='''
<form>
<div class="mt-3">
<input type="text" class="form-control" name="example-text-input" placeholder="Enter value">
</div>
</form>
'''
)
async def make_published_action(self, request: Request, pks: List[Any]) -> str:
# Write your logic here
data: FormData = await request.form()
user_input = data.get("example-text-input")
if ... :
# Display meaningfully error
raise ActionFailed("Sorry, We can't proceed this action now.")
# Display successfully message
return "{} articles were successfully marked as published".format(len(pks))
# For custom response
@action(
name="redirect",
text="Redirect",
custom_response=True,
confirmation="Fill the form",
form='''
<form>
<div class="mt-3">
<input type="text" class="form-control" name="value" placeholder="Enter value">
</div>
</form>
'''
)
async def redirect_action(self, request: Request, pks: List[Any]) -> Response:
data = await request.form()
return RedirectResponse(f"https://example.com/?value={data['value']}")
Source code in starlette_admin/actions.py
row_action(name, text, confirmation=None, action_btn_class=None, submit_btn_class='btn-primary', submit_btn_text=_('Yes, Proceed'), icon_class=None, form=None, custom_response=False, exclude_from_list=False, exclude_from_detail=False)
Decorator to add custom row actions to your ModelView
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Unique row action name for the ModelView. |
required |
text
|
str
|
Action text displayed to users. |
required |
confirmation
|
Optional[str]
|
Confirmation text; if provided, the action will require confirmation. |
None
|
action_btn_class
|
Optional[str]
|
Action button variant for detail page (ex. |
None
|
submit_btn_class
|
Optional[str]
|
Submit button variant (ex. |
'btn-primary'
|
submit_btn_text
|
Optional[str]
|
Text for the submit button. |
lazy_gettext('Yes, Proceed')
|
icon_class
|
Optional[str]
|
Icon class (ex. |
None
|
form
|
Optional[str]
|
Custom HTML to collect data from the user. |
None
|
custom_response
|
Optional[bool]
|
Set to True when you want to return a custom Starlette response from your action instead of a string. |
False
|
exclude_from_list
|
bool
|
Set to True to exclude the action from the list view. |
False
|
exclude_from_detail
|
bool
|
Set to True to exclude the action from the detail view. |
False
|
Usage
@row_action(
name="make_published",
text="Mark as published",
confirmation="Are you sure you want to mark this article as published ?",
icon_class="fas fa-check-circle",
submit_btn_text="Yes, proceed",
submit_btn_class="btn-success",
action_btn_class="btn-info",
)
async def make_published_row_action(self, request: Request, pk: Any) -> str:
session: Session = request.state.session
article = await self.find_by_pk(request, pk)
if article.status == Status.Published:
raise ActionFailed("The article is already marked as published.")
article.status = Status.Published
session.add(article)
session.commit()
return f"The article was successfully marked as published."
Source code in starlette_admin/actions.py
link_row_action(name, text, action_btn_class=None, icon_class=None, exclude_from_list=False, exclude_from_detail=False)
Decorator to add custom row link actions to a ModelView for URL redirection.
Note
This decorator is designed to create row actions that redirect to a URL, making it ideal for cases where a row action should simply navigate users to a website or internal page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Unique row action name for the ModelView. |
required |
text
|
str
|
Action text displayed to users. |
required |
action_btn_class
|
Optional[str]
|
Action button variant for detail page (ex. |
None
|
icon_class
|
Optional[str]
|
Icon class (ex. |
None
|
exclude_from_list
|
bool
|
Set to True to exclude the action from the list view. |
False
|
exclude_from_detail
|
bool
|
Set to True to exclude the action from the detail view. |
False
|
Usage