Wagtail-approach for comments
Asked Answered
C

1

9

We have a blog-like wagtail site and would like to add comments to our post types. Each post is a page object.

We thought about using django-contrib-comments or implement an own plain django comments app with ajax.

But what would be the "all-wagtail-approach" for having a comment functionality on the public wagtail site (only for logged in wagtail users, using ajax)?

We're not looking for a complete implementation, we just need some hints or tips for a wagtail-sensible approach.

Our actual approach is having comments available in in wagtail admin as an InlinePanel on every PostPage. But we're struggling to render a django form for adding new comments on the frontend:


# 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 %}

But: The form ({{ form.as_p }}) is not rendered - any hints? A django admin for PostPageComments works as expected.

Clientage answered 11/12, 2016 at 9:26 Comment(2)
As wagtail pages are django models I do not see any problems with django-contrib-comments.Lurid
Using django-contrib-comments(-xtd) would be my last resort, but I really want to try it with pure wagtail and django. Therefore, I updated my question to show my actual implementation.Clientage
C
6

Some minor changes to my model and template and I have my simple comment form (code not mentioned is unchanged in relation to the question; unrelated code omitted for brevity):

# 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>
Clientage answered 2/1, 2017 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.