django-markdown-editor
django-markdown-editor copied to clipboard
Not working in admin with autocomplete_fields
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
+1
It maybe a namespace conflict with select2. Django uses select2 for admin autocomplete fields.
Any fix for this yet?
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']
Hello @eriktelepovsky, seems this issue has been resolved in the latest version.
Try to upgrade your martor version:
pip install martor --upgrade
Let me know if this issue still persists.