无法将我的应用程序与Django中的文本编辑器集成

时间:2019-05-29 22:44:55

标签: python django richtext

尽管我已经尝试过一些诸如tinymce的富文本编辑器,但是我无法使其工作,没有错误出现,页面正常加载,似乎填充了字段,但是没有任何编辑选项。

尝试使用我可以在Google中找到的几种方法,但是没有一种方法可以帮助我

----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',
    ]

没有错误消息,只是不担心,您能帮我吗?

1 个答案:

答案 0 :(得分:0)

Django CKEditor对我来说很好用。这是我必须做的一个例子:

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