自定义 django-comments

发布于 2024-08-13 08:09:36 字数 1364 浏览 3 评论 0原文

所以,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2024-08-20 08:09:36

评论模型使用通用外键来映射到发表评论的对象,例如博客条目。这些是标准评论表单中包含的必填隐藏字段。

来自 django.contrib.comments.models

...
class CommentSecurityForm(forms.Form):
    """
    Handles the security aspects (anti-spoofing) for comment forms.
    """
    content_type  = forms.CharField(widget=forms.HiddenInput)
    object_pk     = forms.CharField(widget=forms.HiddenInput)
...

如果您没有更改表单类并且只想更改 html 模板,那么您可以通过在所有隐藏字段上添加 for 循环来包含这些字段。

{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}

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

...
class CommentSecurityForm(forms.Form):
    """
    Handles the security aspects (anti-spoofing) for comment forms.
    """
    content_type  = forms.CharField(widget=forms.HiddenInput)
    object_pk     = forms.CharField(widget=forms.HiddenInput)
...

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.

{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}
很酷不放纵 2024-08-20 08:09:36

通过复制 Theju 的应用 - 特别是,请参阅 Joshua Works 对第 2 部分的评论。

Fixed the problem by copying from Theju's app - in particular, see Joshua Works' comment on part 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文