pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.75k stars 1.57k forks source link

Define custom media (CSS/JS) per ModelView #1328

Closed lucasvo closed 7 years ago

lucasvo commented 8 years ago

Django offers a neat way to define JS/CSS dependencies that get loaded automatically when rendering the form: https://docs.djangoproject.com/en/1.10/topics/forms/media/

If I wanted to have something similar in flask-admin, I'd have to extend 4 templates:

      # Templates
    list_template = 'admin/model/list.html'
    """Default list view template"""

    edit_template = 'admin/model/edit.html'
    """Default edit template"""

    create_template = 'admin/model/create.html'
    """Default create template"""

    details_template = 'admin/model/details.html'
    """Default details view template"""

Or is there an easier way to achieve this? If not, is there general demand for supporting this?

mrjoes commented 8 years ago

There's no easy way of doing it right now. But pull requests are welcome!

vgavro commented 7 years ago

It's easy to extend: just drop this template to /templates/admin/master.html

{% extends 'admin/base.html' %}
{% block tail_js %}
  {{ super() }}
  {% if admin_view.extra_js %}
    {% for js in admin_view.extra_js %}
      <script src="{{js}}"></script>
    {% endfor %}
  {% endif %}
{% endblock %}

And add this to your ModelView:

class MyModelView(ModelView):
    extra_js = ['//cdn.ckeditor.com/4.6.0/standard/ckeditor.js']

Anyway, I agree this would be better to have in flask-admin base templates.

petrus-jvrensburg commented 7 years ago

Fixed in #1400

alxvallejo commented 6 years ago

@vgavro I know this is old but I'm getting an error when trying to load a local script from my static dir:

In ModelView: extra_js = [url_for('static', filename='admin/admin.js')]

Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

ghost commented 5 years ago

It's possible to add extra_js in API doc ?

michaelbukachi commented 4 years ago

@alxvallejo if you haven't found a solution yet, checkout this answer. It's also explained in the documentation.