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

2

5

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?

Alienation answered 17/3, 2019 at 21:52 Comment(0)
A
6

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))
Alienation answered 17/3, 2019 at 22:49 Comment(0)
A
0

As for DOC, this is the default command:

admin = Admin(app, name='microblog', template_mode='bootstrap3')

Add your own CSS here /static/css/main.css:

{% block head_css %}
  {{ super() }}
  <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css', _external=True) }}" ></link>
{% endblock %}
Atherosclerosis answered 15/9, 2020 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.