我们有一个类似博客的摇尾网站,并希望添加评论到我们的帖子类型。每个帖子都是一个page对象。
我们考虑使用django-评语或使用ajax实现自己的普通django注释应用程序。
但是,在公共摇尾站点上拥有评论功能的“全摇式方式”(只适用于使用ajax登录的摇尾用户)是什么呢?
我们并不是在寻找一个完整的实现,我们只是需要一些提示或提示来进行一种明智的方法。
我们的实际方法是在摇尾管理中有注释作为InlinePanel在每个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管理员就像预期的那样工作。
发布于 2017-01-02 22:58:09
对我的模型和模板做了一些小的修改,我有一个简单的注释表单(对于这个问题,没有提到的代码没有变化;为了简洁起见,省略了不相关的代码):
# 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>https://stackoverflow.com/questions/41084713
复制相似问题