Wagtail方法征求意见

时间:2016-12-11 09:26:57

标签: django wagtail

我们有一个像博客一样的wagtail网站,并希望为我们的帖子类型添加评论。每个帖子都是page个对象。

我们考虑过使用django-contrib-comments或使用ajax实现自己的普通django评论应用。

但是,在公共网站上使用评论功能会有什么“令人讨厌的方法”(仅限登录wagtail用户,使用ajax)?

我们不是在寻找完整的实施方案,我们只需要一些提示或提示,以便采取令人讨厌的方法。

我们的实际方法是在每个InlinePanel上以w PostPage的形式提供评论。但我们正在努力渲染一个django形式,以便在前端添加新评论:

# blog/models.py

class PostPage(RoutablePageMixin, Page):
    ...field definitions...

    @route(r'^comment/new/$')
    def add_comment_to_post(self, request):
        from .forms import CommentForm

        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                comment = form.save()
                return render(request, self.template, {
                    'page': self,
                    'comment': comment,
                })
        else:
            form = CommentForm()

        return render(request, self.template, {
            'page': self,
            'form': form,
        })
    content_panels = Page.content_panels + [
        ...FieldPanels...
        InlinePanel('comments', label="Comments"),
    ]

class Comment(models.Model):
    text = models.TextField()

    panels = [FieldPanel('text'),]

    def __str__(self):
        return self.text

    class Meta:
        abstract = True


class PostPageComments(Orderable, Comment):
    page = ParentalKey('PostPage', related_name='comments')
# blog/forms.py
from django import forms
from .models import PostPageComments


class CommentForm(forms.ModelForm):

    class Meta:
        model = PostPageComments
        fields = ['text']
# blog/templates/blog/post_page.html

{% extends "core/base.html" %}
{% load wagtailcore_tags %}

{% block content %}

    {% include 'stream/includes/post_list_item.html' with include_context="post_detail_page" post=self %}

    <h3>New comment</h3>
    <form method="post" action="comment/new/" id="comment-new">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Send</button>
    </form>

{% endblock %}

但是:form{{ form.as_p }})未呈现 - 任何提示? PostPageComments的django管理员按预期工作。

1 个答案:

答案 0 :(得分:2)

对我的模型和模板进行了一些小的更改,并且我有一个简单的注释表单(未提及的代码与问题相关;为简洁起见省略了不相关的代码):

# blog/models.py
class PostPage(Page):

    def serve(self, request):    
        from .forms import CommentForm

        if request.method == 'POST':
            form = CommentForm(request.POST)

            if form.is_valid():
                comment = form.save(commit=False)
                comment.page_id = self.id
                comment.save()
                return redirect(self.url)
        else:
            form = CommentForm()

        return render(request, self.template, {
            'page': self,
            'form': form,
        })

class Comment(models.Model):
    text = models.TextField()

    class Meta:
        abstract = True

class PostPageComments(Orderable, Comment):
    page = ParentalKey('PostPage', related_name='comments')
# blog/templates/blog/post_page.html

<form method="post" id="comment-new">
    {% csrf_token %}
    {{ form.as_p }}     
    <button type="submit">Send</button>
</form>