Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating custom views for BaseView grouped in a category and passing an id #2422

Open
Acrofil opened this issue Apr 3, 2024 · 1 comment

Comments

@Acrofil
Copy link

Acrofil commented Apr 3, 2024

Hi, i try to achieve something that should be simple to do but i cant.

class ObjectDataView(BaseView):
    @expose('/')
    def index(self):
        return self.render('path/to/index.html')
    
    @expose('/<int:object_id>/')
    def object_data(self, object_id):
        object = Objects.query.get(object_id)
        return self.render('path/to/object_render_data.html', object=object')

# Get all objects and group them under 1 category
# Each object then will get his own page where we render the data it contains
objects = Objects.query.all()
for object in objects:
    admin.add_view(ObjectDataView(name=object.name,
                            category='Objects',
                            endpoint=f'object_data/{object.id}',
                            url=f'object_data/{object.id}',)

With the code above, what I achieve is:
Category is created with all objects and when I select any I get routed to:
http://127.0.0.1:5000/admin/object_data/99/ (or any other ID from the selected in the dropdown)/ which renders the index.html
if I manually edit the URL to include another ID:
http://127.0.0.1:5000/admin/object_data/99/99/ - I get routed to object_render_data.html Which is what i want to achieve in the first place.

If i change the url url='object_data' i get redirected again to the index and if i add manually the object id object_data/99 i will go to the page i want.

I also tried creating them as menu links. Then I get to the specified route and receive the id, but then if i want to extend the flask admin template, I can't and i dont really want to do it that way.

custom_bp = Blueprint('custom_bp', __name__)

@custom_bp.route("/admin/custom_route/<int:object_id>", methods=['GET', 'POST'])
def room_availability_test(object_id):
    return render_template('path/to/object_render_data.html')

admin.add_link(MenuLink(name=...., 
category=...., 
url=f'custom_route/{object.id}

What I am missing or doing wrong?
Any help appreciated!
Thanks!

@samuelhwilliams
Copy link
Contributor

It looks like you're creating an data view for each individual object - you don't need to do this. You create a single object data view, and it will work for any instance of your Object class.

Note: untested code, but should be about right.

class ObjectDataView(BaseView):
    @expose('/')
    def index(self):
        return self.render('path/to/index.html')
    
    @expose('/<int:object_id>/')
    def object_data(self, object_id):
        object = Objects.query.get(object_id)
        return self.render('path/to/object_render_data.html', object=object)

admin.add_view(
    ObjectDataView(
        name='Objects',
        url=f'object_data/',
    )
)

It's possible you're intentionally doing what you're doing, in order to have a link for each specific object in the title bar, but I think this is probably not the right approach. Have you tried something more like what I've got above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants