Skip to content

Using django-tinymce in inline editing mode #463

@philgyford

Description

@philgyford

The django-tinymce documentation doesn't mention using it for inline editing mode. The only issue I've found about it (#135) was closed as "resolved" but with no info. Is it possible to use it for this purpose?

I feel like I've got close, but it requires some custom JavaScript, and it doesn't pick up any of the django-tinymce settings from settings.py.

I have a Django form:

class TestModelInlineUpdateForm(forms.ModelForm):
    class Meta:
        model = TestModel
        fields = ("description",)
        widgets = {"description": TinyMCE()}

And then this in my template:

  <form method="post" id="testForm">
    {% csrf_token %}
    {{ form.media }}

    <div class="tinymce" id="inline-editor">
      {{ object.description|safe }}
    </div>

    {% for hidden in form.hidden_fields %}
      {{ hidden }}
    {% endfor %}

    <input type="hidden" name="description" value="">

    <button type="submit">Update</button>
  </form>

  <script type="text/javascript">
    tinymce.init({
      selector: '#inline-editor',
      inline: true
    });

    // Set the description field to the new HTML on form submission.
    document.querySelector("#testForm").addEventListener("submit", function(ev) {
      ev.preventDefault();
      let descriptionHTML = document.querySelector("#inline-editor").innerHTML;
      let descriptionField = document.querySelector("input[name='description']");
      descriptionField.value = descriptionHTML;
      this.submit();
    });
  </script>

I have to manually copy the HTML from the editor to my manually-added hidden form field when the form is submitted. It works, but is clunky. And, as I say, django-tinymce's settings are ignored.

Have I missed an easier, better way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions