django-markdown-editor icon indicating copy to clipboard operation
django-markdown-editor copied to clipboard

Not working in admin with autocomplete_fields

Open eriktelepovsky opened this issue 5 years ago • 4 comments
trafficstars

Hi. I noticed Martor editor works fine until there is autocomplete_fields defined in the admin:

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    search_fields = ('title', 'category__title', 'content')
    date_hierarchy = 'created'
    list_display = ('title', 'category', 'created', 'modified')
    list_filter = ('category',)
    list_select_related = ['category']
    # autocomplete_fields = ['category']  # not working with martor

Console error:

Uncaught TypeError: $(...).dropdown is not a function
    at HTMLDocument.<anonymous> (martor.js:848)
    at mightThrow (jquery.js:3535)
    at process (jquery.js:3603)

Config:

MARTOR_ENABLE_CONFIGS = {
    'spellcheck': 'false',
    'imgur': 'true',     # to enable/disable imgur/custom uploader.
    'mention': 'false',  # to enable/disable mention
    'jquery': 'true',    # to include/revoke jquery (require for admin default django)
}

Tested on versions:

Django==2.2.6
Markdown==3.1.1
martor==1.4.4

eriktelepovsky avatar Nov 26 '19 15:11 eriktelepovsky

+1

virusdefender avatar Mar 18 '20 09:03 virusdefender

It maybe a namespace conflict with select2. Django uses select2 for admin autocomplete fields.

onurmatik avatar Apr 21 '20 18:04 onurmatik

Any fix for this yet?

eriktelepovsky avatar Jun 08 '20 13:06 eriktelepovsky

Might be related to the dropdown function in the jQuery library. Load jquery before the martor editor or Martor's JavaScript should be loaded before the jQuery import in your HTML template.

{% extends "admin/base.html" %}

{% block extrahead %}
    {{ block.super }}
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    {{ form.media }}
{% endblock %}

{% block content %}
    {{ block.super }}
    {{ form.as_p }}
{% endblock %}
    {% load static %}
    <script type="text/javascript" src="{% static 'path/to/martor.js' %}"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

Disabling the dropdown function in Martor by disabling the 'auto_urlize' option in the MARTOR_ENABLE_CONFIGS setting.

MARTOR_ENABLE_CONFIGS = {
'auto_urlize': 'false'
}

Disabling the autocomplete_fields option for the Article model in the Django admin, as it seems to be the root cause of the issue. You can do this by commenting out the line autocomplete_fields = ['category'] in the ArticleAdmin class:

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    search_fields = ('title', 'category__title', 'content')
    date_hierarchy = 'created'
    list_display = ('title', 'category', 'created', 'modified')
    list_filter = ('category',)
    list_select_related = ['category']
    # autocomplete_fields = ['category']  

some1ataplace avatar Apr 05 '23 01:04 some1ataplace

Hello @eriktelepovsky, seems this issue has been resolved in the latest version.

Screenshot 2024-03-30 at 15 58 34

Try to upgrade your martor version:

pip install martor --upgrade

Let me know if this issue still persists.

agusmakmun avatar Mar 30 '24 08:03 agusmakmun