如何在模板django中以脆弱的形式显示ckeditor?

时间:2018-01-18 08:59:25

标签: python django ckeditor django-crispy-forms

我已经为django安装了ckeditor,它会在管理员中显示,但是如何让它显示在我的模板中。

模板

{% extends 'blog/base.html' %} {% load crispy_forms_tags %}{%block content%} {% if user.is_superuser %}
<div class="container">
    <div class="row">

        <div class="col-md-6 col-md-7">
            <div class="panel panel-default">
                <div class="panel-body">
                    {% if error_message %}
                    <p><strong>{{ error_message }}</strong></p>
                    {% endif %}
                    <form class="form-horizontal" id="label-edit" role="form" action="" method="post" enctype="multipart/form-data">
                        {% csrf_token %} {{ form|crispy }}
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    {% else %}
    <h1 class="display-4 text-center">404 Not Found</h1>
    {% endif %} {% endblock %}

我正在使用脆皮形式。我希望ckeditor出现在模板上。提前谢谢。

3 个答案:

答案 0 :(得分:1)

您需要显式添加{{form.media}},使其看起来像 _

答案 1 :(得分:0)

在表单类文件中添加以下内容:

{"iterable":["foo","bar"],"collection":["foo","bar"],"list":["foo","bar"],"map":{"foo":"bar"}}

然后您可以像这样使用CKEditorWidget:

from ckeditor.widgets import CKEditorWidget

有关更多信息,请阅读git page ....

上的指南

答案 2 :(得分:0)

要使 CKEditor 在管理面板之外工作,您必须通过执行以下操作来确保所有表单媒体都存在以供编辑器工作:

...
{{ form.media }}
{{ form.as_p }}
// or you can use crispy filter: {{ form|crispy }}
// you can specify the exactly field you want e.g form.content
{{ form.content }}
// you can use crispy without any problem: 
{{ form.content|as_crispy_field }}
...

有时,当使用 {{ form.media }} 时它不会加载媒体并显示错误(对我来说它给了我这个错误:Object of type PosixPath is not JSON serializable),所以你必须在 {{ 1}} 文件(或您称之为的任何文件,例如:form.html)或通过 blog_form.html 标记的 base.html 文件:

<script>

有时你不得不提到 ckeditor 会在 {% load static %} ... <script type="text/javascript" src="{% static "ckeditor/ckeditor-init.js" %}"> </script> <script type="text/javascript" src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script> 文件中通过 CKEDITOR_BASEPATH 在哪里找到它的静态文件:

settings.py

查看 CKEditor 文档了解更多信息