我所做的基于此: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 %}


但是它显示如下:
python - Django-summernote:如何使它基于模型形式工作?-LMLPHP

我想我只是将form传递给模板,但是在README中,它访问了fieldfoobar|safe。。实际上,我认为它不应该是foobar,因为该字段是foobar。 )

请您帮忙。谢谢

最佳答案

在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

关于python - Django-summernote:如何使它基于模型形式工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38988173/

10-12 18:39