django – Wagtail-comments评论

我们有一个类似博客的wagtail网站,并希望为我们的帖子类型添加评论.每个帖子都是一个页面对象.

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

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

我们不是在寻找一个完整的实现,我们只需要一些提示或技巧来制定一个令人讨厌的方法.

我们的实际方法是在wigtail admin中将注释作为每个PostPage上的InlinePanel提供.但我们正在努力渲染一个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.as_p}})没有呈现 – 任何提示? PostPageComments的django管理员按预期工作.

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

# 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>
点赞