渲染ckeditor小部件与django模板中的模型字段

时间:2014-04-16 16:19:42

标签: django django-forms django-templates ckeditor

Iam尝试使用模型字段和ckeditor wiget创建模板但是我无法在html中呈现字段,而是显示空白页面而没有错误

这是我的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 %}

此外,我已将“ckeditor&#39;,app”添加到settings.py,并以正确的方式配置了所有静态网址。

当我只尝试在模板中渲染ckeditor-widget时,我没有问题,它是完美的

空白 - write_post.html(输出)

enter image description here

2 个答案:

答案 0 :(得分:3)

编辑表单并添加form.media,如下所示:

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

答案 1 :(得分:1)

仅供记录。我也有同样的问题。由于低端python版本,需要更好的稳定版本的Django。只需使用pip django-ckeditor安装即可。这应该足够了。如果没有,请安装Django == 1.8.16