自定义 django-comments
所以,我正在使用 django.contrib.comments。我已经安装好了,但我不想使用笨拙的默认评论表单,而是想使用仅显示文本区域和提交按钮的自定义表单模板。
其背后的理由是,用户只有在已经通过身份验证的情况下才能看到表单,并且我希望保持表单简单并自动获取他们的用户名等。
我已经实现了自定义表单,但是当我尝试提交它时遇到错误。
这是我在带有评论表单的页面模板中的内容(条目是从视图传递的对象):
{% load comments %}
{% render_comment_form for entry %}
这是 /templates/comments/form.html 中的 HTML:
{% if user.is_authenticated %}
<p>Submit a comment:</p>
<form action="/comments/post/" method="post">
<textarea name="comment" id="id_comment" rows="2" style="width: 90%;"></textarea>
<input type="hidden" name="options" value="{{ options }}" />
<input type="hidden" name="target" value="{{ target }}" />
<input type="hidden" name="gonzo" value="{{ hash }}" />
<input type="hidden" name="next" value="{{ entry.get_absolute_url }}" />
<span style="float:right;"><input type="submit" name="post" value="Add"></span>
</form>
{% else %}
<p>Please <a href="/login/">log in</a> to post a comment.</p>
{% endif %}
它最初呈现正常,但是当我尝试提交时在评论表单中,我收到以下 Django 错误:
Comment post not allowed (400)
Why: Missing content_type or object_pk field.
有人能帮忙吗?
So, I'm using django.contrib.comments. I've installed it OK but rather than the unwieldy default comment form, I'd like to use a custom form template that just shows a textarea and submit button.
The rationale behind this is that user only see the form if they area already authenticated, and I'd like the keep the form simple and pick up their username etc automatically.
I've implemented a custom form, but am getting an error when I try to submit it.
Here's what I have in my template for the page with the comment form (entry is the object passed from the view):
{% load comments %}
{% render_comment_form for entry %}
And here's my HTML in /templates/comments/form.html:
{% if user.is_authenticated %}
<p>Submit a comment:</p>
<form action="/comments/post/" method="post">
<textarea name="comment" id="id_comment" rows="2" style="width: 90%;"></textarea>
<input type="hidden" name="options" value="{{ options }}" />
<input type="hidden" name="target" value="{{ target }}" />
<input type="hidden" name="gonzo" value="{{ hash }}" />
<input type="hidden" name="next" value="{{ entry.get_absolute_url }}" />
<span style="float:right;"><input type="submit" name="post" value="Add"></span>
</form>
{% else %}
<p>Please <a href="/login/">log in</a> to post a comment.</p>
{% endif %}
It renders okay initially, but when I try to submit the comment form, I get the following Django error:
Comment post not allowed (400)
Why: Missing content_type or object_pk field.
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
评论模型使用通用外键来映射到发表评论的对象,例如博客条目。这些是标准评论表单中包含的必填隐藏字段。
来自 django.contrib.comments.models
如果您没有更改表单类并且只想更改 html 模板,那么您可以通过在所有隐藏字段上添加 for 循环来包含这些字段。
The comment model uses a generic foreign key to map to the object for which the comment was made such as a blog entry. These are required hidden fields included in the standard comment form.
From django.contrib.comments.models
If you haven't changed the form class and only want to change the html template then you can include these fields by adding a for loop over all the hidden fields.
通过复制 Theju 的应用 - 特别是,请参阅 Joshua Works 对第 2 部分的评论。
Fixed the problem by copying from Theju's app - in particular, see Joshua Works' comment on part 2.