jowilf / starlette-admin

Fast, beautiful and extensible administrative interface framework for Starlette & FastApi applications
https://jowilf.github.io/starlette-admin/
MIT License
568 stars 59 forks source link

Bug: Docs could illustrate adding a custom button to a custom view #570

Closed sglebs closed 1 week ago

sglebs commented 3 weeks ago

Describe the bug

I have a table with records showing events. It is quite raw/basic. I would like to add an extra action button in the List View that, when clicked, takes me o a nicer view which will show me a custom historic view of related records (I have the query). It is a form of drill-down with a beautiful rendering.

I have been able to create a custom view for the home page in the past and a custom row action button that opens a dialog and executes code. But I am not suer how to make an action button take me to a custom view which is not normally available on the left menu/navigator. Any pointers?

To Reproduce

Create a custom action button which is shown in the List View and get it to open a custom view on that entity, with a custom template of yours, much like the eye or pencil icons.

Environment (please complete the following information):

Additional context I am browsing the sources to find hints.

rperetti commented 3 weeks ago

I believe you can do the following:

When initializing the admin you define your custom view :

admin.add_view(CustomView(label="My Custom View", icon="custom_icon", path="/my_custom_view", template_path="my_custom_view.html", add_to_menu=True))

And you can all it from your custom action like this:

    @row_action(
        name="my_custom_action",
        text="My Custom Action",
        custom_response=True,
        icon_class="custom_icon",
    )
    async def my_custom_action(self, request: Request, pks: List[Any]) -> Response:
        url = "/admin/my_custom_view"  # ideally we should not hardcode the path - can we get the path to a view?
        return RedirectResponse(url)
jowilf commented 2 weeks ago

ideally we should not hardcode the path - can we get the path to a view?

If you set the name attribute for your customview :

admin.add_view(CustomView(name="custom_view", ...))

you can generate the url with

request.url_for("admin:custom_view"))
jowilf commented 1 week ago

this is already in the doc -> https://jowilf.github.io/starlette-admin/user-guide/actions/