romanvm / django-tinymce4-lite

TinyMCE 4 editor widget for Django
MIT License
126 stars 47 forks source link

Editor content not saved when using form.serialize() #23

Closed will-emmerson closed 6 years ago

will-emmerson commented 6 years ago

I had a bug where a form field using HTMLField() wasn't submitting the changed value. I finally tracked it down to using form.serialize() to send form via ajax - this issue: https://stackoverflow.com/q/2122085

The solution was to use tinyMCE.triggerSave(); before submitting. I realise this isn't an issue with django-tinymce4-lite but just wondering if it's worth noting in documentation? Or integrating solution from https://stackoverflow.com/a/24284938 to avoid the problem:

tinymce.init({
    selector: "textarea",
    setup: function (editor) {
        editor.on('change', function () {
            editor.save();
        });
    }
});
jaredaus commented 6 years ago

This didn't work for me.

Unsure if it was after upgrading to Bootstrap 4 or using the form in the frontend (as opposed to the admin) but I couldn't get the form to save.

Also, I'm fairly green so could be completely unrelated!

Ended up using this subclassing via the commented blog post.

forms.py

"""
    https://fosstack.com/how-to-set-up-tinymce-in-django-app/
"""
class TinyMCEWidget(TinyMCE):
    def use_required_attribute(self, *args):
        return False 

...

class JobForm1(forms.ModelForm):
    class Meta:
        model = Job

        fields = (
                'name',
                'company',
                'description',
                'urlapply',
                'emailapply',
            )
        description = forms.CharField(widget=TinyMCEWidget(mce_attrs={'width': 800}))

...