jazzband / django-tinymce

TinyMCE integration for Django
http://django-tinymce.readthedocs.org/
MIT License
1.28k stars 317 forks source link

Empty field value when loaded by HTMX #446

Open H4rryK4ne opened 1 year ago

H4rryK4ne commented 1 year ago

I don't get the content of the HTMLField if I use this in a dynamically loaded view with HTMX.

StackOverflow seems to have already some questions regarding this issue:

Unfortunately, my JavaScript skills are not yet developed enough, to apply any of the proposed solutions (This is also the reason, why I was very happy to find HTMX, which solves a lot of my issue, where JS would be required)

416 mentions a similar issue, but with bootstrap

sdolemelipone commented 10 months ago

The fix to one of the questions you linked to will solve your problem: https://stackoverflow.com/a/24284657/1785448.

django-tinymce knows to automatically evaluate javascript functions if they are defined in python strings for specific configuration options, like "setup". So in your settings.py:

# All of the below are the defaults from django-tinymce's documentation 
TINYMCE_DEFAULT_CONFIG = {
    "theme": "silver",
    "height": 500,
    "menubar": False,
    "plugins": "advlist,autolink,lists,link,image,charmap,print,preview,anchor,"
    "searchreplace,visualblocks,code,fullscreen,insertdatetime,media,table,paste,"
    "code,help,wordcount",
    "toolbar": "undo redo | formatselect | "
    "bold italic backcolor | alignleft aligncenter "
    "alignright alignjustify | bullist numlist outdent indent | "
    "removeformat | help",
    # Add a setup function to apply a fix for HTMX:
    "setup": "function (editor) {editor.on('change', function () {tinymce.triggerSave();});}",
}