django-ckeditor {{ form.media }} 在 html 中不工作

django-ckeditor {{ form.media }} not working in html

我正在尝试从 forms.py 获取 ck-editor 外观。
阅读文档甚至在 YouTube 上后,我没有得到我想要的结果。
我正在共享我的不同文件。
请解决这个问题。

HTML FILE

<form method="post" enctype="multipart/form-data">
   <div class="col me-2">
          {% csrf_token %}
          {{ notes_form.media }}
          {{ notes_form.as_p }}
   </div>
   <div class="col-auto">
      <button class="btn btn-outline-success btn-sm border rounded-pill border-success float-end todo-submit" type="button">
            <i class="fas fa-check"></i>
        </button>
    </div>
</form>

Form.py

class NotesForm(ModelForm):
    class Meta:
        model = Notes
        fields = ['description']

    def __init__(self, *args, **kwargs):
        super(NotesForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs.update({
                'class': 'form-control'
            })

views.py

def index(request):
    if request.user.is_authenticated:
        params = {
            "notes_form": NotesForm()
        }
        return render(request, 'dashboard.html', params)
    else:
        return render(request, 'home.html')

output (html file)

output image

实际上我得到了解决方案。

当您调用 {{ notes_form.media }} 时,它会加载脚本文件,即 <script src="/assets/ckeditor/ckeditor-init.js"></script><script src="/assets/ckeditor/ckeditor/ckeditor.js"></script>。由于这个 JavaScript 首先加载并且它没有得到出现的文本区域(由于)在此之后 {{ notes_form.as_p }}

HTML file

<form method="post" enctype="multipart/form-data">
   <div class="col me-2">
          {% csrf_token %}
          {{ notes_form.media }}
          {{ notes_form.as_p }}
   </div>
   <div class="col-auto">
      <button class="btn btn-outline-success btn-sm border rounded-pill border-success float-end todo-submit" type="button">
            <i class="fas fa-check"></i>
        </button>
    </div>
</form>

updated HTML file

<form method="post" enctype="multipart/form-data">
   <div class="col me-2">
          {% csrf_token %}
          {{ notes_form.as_p }}
          {{ notes_form.media }}
   </div>
   <div class="col-auto">
      <button class="btn btn-outline-success btn-sm border rounded-pill border-success float-end todo-submit" type="button">
            <i class="fas fa-check"></i>
        </button>
    </div>
</form>