htmldjangodjango-ckeditor

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


I am trying to get ck-editor look from forms.py .
After reading docs and even on YouTube, I didn't get my desired result.
I am sharing my different file.
Please solve this.

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


Solution

  • Actually I got the solution.

    when you call {{ notes_form.media }}, it load script files i.e. <script src="/assets/ckeditor/ckeditor-init.js"></script><script src="/assets/ckeditor/ckeditor/ckeditor.js"></script>. Due to this JavaScript load first and it didn't get the textarea which appear (due to) {{ notes_form.as_p }} after this.

    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>