Search code examples
pythondjangorichtext

Can not integrate my app with a text editor in django


Although I have tried several richtext editors like tinymce, but I am not being able to make it work, no error appears, the page loads normally, a field appears to be filled, but without any editing option.

Try to use several ways I could find in google, but none was able to help me

----base.html

<head>
...

<!-- Tinymce Text-Editor (Must be in Head Tag) -->
  <script src="{% static '/tinymce/js/tinymce/tinymce.min.js' %}"></script>
  <script type="text/javascript" src="{% static 'js/custom.js' %}" ></script>
...

</head>
---post_form.html

{% extends "blog/base.html" %}
<!-- {% load crispy_forms_tags %} -->
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Blog Post</legend>
                <!-- {{ form|crispy }} -->
                {{ form.as_p }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
            </div>
        </form>
    </div>
{% endblock content %}
---- models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from simple_history.models import HistoricalRecords
from tinymce.models import HTMLField


class Post(models.Model):
    title = models.CharField(max_length = 100)
    versao = models.CharField(max_length=10, default=1)
    # content = models.TextField()
    content = HTMLField()
    resumo_das_mudancas = models.TextField(default='Não houve mudanças')
    date_posted = models.DateTimeField(default = timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs ={'pk':self.pk})

.....

------settings.py

...

INSTALLED_APPS = [
    'django.contrib.sites',
    'django.contrib.staticfiles',
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.flatpages',
    'simple_history',
    'blog.apps.BlogConfig',
    'users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.sessions',
    'django.contrib.messages',
    'crispy_forms',
    'storages',
    'tinymce',
    ]

There are no error messages, it just does not wor, can you help me?


Solution

  • Django CKEditor works very well for me. Here's an example what I had to do:

    requirements.txt

    django-ckeditor==5.6.1
    

    settings.py

    INSTALLED_APPS = [
       ...
       'ckeditor'
    ]
    
    CKEDITOR_CONFIGS = {
        'default': {
            'toolbar': 'Custom',
            'toolbar_Custom': [
                ['Bold', 'Italic', 'Underline'],
                ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
                ['Link', 'Unlink'],
                ['RemoveFormat', 'Source']
            ],
            'height': 300,
            'width': 600
        }
    }
    

    models.py

    class ExampleModel(models.Model):
        ....
        example_field = models.TextField(null=True, blank=True)
    

    admin.py

    from ckeditor.widgets import CKEditorWidget
    
    class ExampleForm(forms.ModelForm):
        class Meta:
            model = ExampleModel
            fields = '__all__'
            widgets = {'example_field': CKEditorWidget()}
    
    class ExampleAdmin(admin.ModelAdmin):
        form = ExampleForm