Steam Engine
Steam Engine

Reputation: 60

Rendering ckeditor widget with model fields in django template

Iam trying to create a template with model fields and a ckeditor wiget but Iam unable to render fields in the html, and instead I am shown a blank page without error

Here is my forms.py

 from django import forms
 from app.models import BlogPost
 from ckeditor.widgets import CKEditorWidget

class BlogForm(forms.ModelForm):
    title = forms.CharField(required=True)
    body = forms.CharField(widget=CKEditorWidget())
    tags = forms.CharField(required=True)


    class Meta:
        model = BlogPost

models.py

from django.db import models
from ckeditor.fields import RichTextField


class BlogPost(models.Model):
    title = models.CharField(max_length=70)
    body = RichTextField()
    tags = models.CharField(max_length=10)

views.py

def write_post(request):
    blog_form=BlogForm()
    return render(request,'write_post.html',{'blog_form':blog_form})

write_post.html

{%block content%}
{% load staticfiles %}<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script>
</head>

<div>
    <h1>Create a Post</h1>

    <form action="/submit" method="post">
        {% csrf_token %}
        <div>
           {{ form.title }}
           {{ form.body }}
           {{ form.tags }}
        </div>
        <input type="submit" value="Post">
     </form>
 </div>

 {% endblock %}

I addition I have added 'ckeditor', app to settings.py and configured all the static url's in the right way.

When I only try to render only the ckeditor-widget in a template I have no problem it's all perfect

blank -- write_post.html (output)

enter image description here

Upvotes: 2

Views: 4482

Answers (2)

Michel Gagn&#233;
Michel Gagn&#233;

Reputation: 11

Just for the record. I also had the same problem. Because of a low end python version a better stable version of Django was required. Just install with pip django-ckeditor. This should be sufficient. If not, install Django==1.8.16

Upvotes: 1

Romaan
Romaan

Reputation: 2757

Edit your form and add form.media as shown below:

<form action="/submit" method="post">
    {% csrf_token %}
    <div>
       {{ form.media }}
       {{ form.title }}
       {{ form.body }}
       {{ form.tags }}
    </div>
    <input type="submit" value="Post">
 </form>

Upvotes: 5

Related Questions