我所做的是基于此:https://github.com/summernote/django-summernote
但它没有用。
(我设置了README
)
urls.py
from django.conf.urls import url, include
from .views import PostListView, PostNewView, PostDetailView
app_name = 'posts'
urlpatterns = [
url(r'^$', PostListView.as_view(), name='list'),
url(r'^new/$', PostNewView.as_view(), name='new'),
url(r'^(?P<pk>[0-9]+)/$', PostDetailView.as_view(), name='detail'),
url(r'^summernote/', include('django_summernote.urls')),
]
models.py
from django.db import models
from django.core.urlresolvers import reverse
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse(
"posts:detail",
kwargs={
"pk": self.id,
}
)
forms.py
from django import forms
from django_summernote.widgets import SummernoteInplaceWidget
from posts.models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'content')
widgets = {
'content': SummernoteInplaceWidget(),
}
views.py
from django.views.generic import CreateView
from django.shortcuts import render, redirect
from posts.models import Post
class PostNewView(CreateView):
model = Post
template_name = "posts/post_edit.html"
fields = ['title', 'content']
post_edit.html
{% extends 'chacha_dabang/skeleton/base.html' %}
{% block content %}
<h1> New post</h1>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|safe }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}
我认为我只是将form
传递给了模板,但在README
中,它访问了field
(foobar|safe
。实际上我认为它不应该是foobar
因为该字段为foo
和bar
。)
请你帮忙。感谢
答案 0 :(得分:2)
在PostNewView中,您需要添加form_class
class PostNewView(CreateView):
model = Post
form_class = PostForm
template_name = "posts/post_edit.html"
fields = ['title', 'content']
或者您可以将PostNewView中的get_form方法覆盖为
class PostNewView(CreateView):
model = Post
template_name = "posts/post_edit.html"
fields = ['title', 'content']
def get_form(self, form_class):
form = super(PostNewView, self).get_form(form_class)
form.fields['content'].widget = SummernoteInplaceWidget()
return form