Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wagtail-approach for comments

Tags:

django

wagtail

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.

like image 763
tombreit Avatar asked Dec 11 '16 09:12

tombreit


1 Answers

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>
like image 173
tombreit Avatar answered Nov 08 '22 14:11

tombreit