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.
django-contrib-comments
. – Luriddjango-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