简体   繁体   中英

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?

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))

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM