Search code examples
djangockeditordjango-ckeditor

Loading initial text, into CKEditor


I have integrated CKEditor to my django app, I am able to save the text to my database easily. But i cant find a way to Edit that data. I am unable to find a way to load the text into ckeditor.

models.py

class BlogContent(models.Model):
    emailID = models.EmailField()
    username = models.CharField(max_length=20)
    blogID = UUIDField(auto=True)
    blogHead = models.CharField(max_length=200)
    blogBody = RichTextField()
    blogDateTime = models.DateTimeField(auto_now=True)

form.py

class addBlog(forms.ModelForm):

    class Meta:

        model = BlogContent
        fields = ['blogHead', 'blogBody']
        widget = {'blogBody': CKEditorWidget()}

view.py

def addNewArticle(request):
    form = addBlog()
    args = {"form": form}
    args.update(csrf(request))
    return render_to_response("addNewArticle.html", args)

How do i accomplish that ?


Solution

  • Just instance the Form with a hash inside, that hash must have the next structure:

    {"field_name": "value"}
    

    so just change this line:

     form = addBlog()
    

    for this one:

     form = addBlog({"blogBody": "valueFromDatabase"})
    

    This can works with either ModelForm or Forms, but with ModelForms you can also pass an instance from The Model that the ModelForm uses, so this will also works.

     instance = BlogContent.objects.get(id=1)
     form = addBlog(instance=instance)