Search code examples
pythonpython-3.xflaskflask-admin

How to extend Flask-admin model Edit and Create templates?


according to the flask-admin docs I can extend the main flask-admin dashboard by creating the file templates/admin/index.html and extending admin/master.html. The HTML would look like this:

{% extends 'admin/master.html' %}

{% block body %}
    <h1>HELLO</h1>
{% endblock body %}

But i can't find any information on how to extend the model CRUD pages: List, Edit and Create. I need to extend the Create and Edit User page so i can add js code to the form template.

Is there a template where i can extend just like admin/master.html example?


Solution

  • Just found it in flask-admin docs. I had to create templates/edit_user.html and templates/create_user.html. For list_users is also the same, theres is an example in the docs.

    In edit_user.html

    {% extends 'admin/model/edit.html' %}
    
    {% block body %}
        <h1>User Edit View</h1>
        {{ super() }}
    {% endblock %}
    

    In create_user.html

    {% extends 'admin/model/create.html' %}
    
    {% block body %}
        <h1>Create View</h1>
        {{ super() }}
    {% endblock %}
    

    and then add this to the User model View:

    class UserView(ModelView):
        edit_template = 'edit_user.html'
        create_template = 'create_user.html'
    
    
    admin.add_view(UserView(User, db.session))