Django 的评论应用程序的自定义模板不显示字段

发布于 2024-08-26 11:45:57 字数 1216 浏览 6 评论 0原文

我想在博客应用程序中使用 django.contrib.comments 并自定义表单的显示方式。我的问题是,尽管显示隐藏字段效果很好,但我无法显示字段。我查看了文档并将其与显示表单的常规方式进行了比较,但老实说,我不知道为什么以下内容不起作用:

{% get_comment_form for comments_object as form %}
<form action="{% comment_form_target %}" method="POST">
[…]
{% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}
    {% for field in form.fields %}
        {{field}}
    {% endfor %}
[…]
</form>

输出看起来像这样:

    <form action="/comments/post/" method="POST"> 
            <input type="hidden" name="content_type" value="flatpages.flatpage" id="id_content_type" />         
            <input type="hidden" name="object_pk" value="1" id="id_object_pk" /> 
            <input type="hidden" name="timestamp" value="1269522506" id="id_timestamp" />              
            <input type="hidden" name="security_hash" value="ec4…0fd" id="id_security_hash" /> 
            content_type
            object_pk
            timestamp
            security_hash
            name
            email
            url
            comment
            honeypot
        […]
    </form> 
</div> 

你能告诉我我做错了什么吗?提前致谢

I want to use django.contrib.comments in a blogging application and customize the way the form is displayed. My problem is that I can't get the fields to display although displaying the hidden fields works just fine. I had a look at the docs and compared it with the regular way of displaying forms but honestly I don't know why the following doesn't work out:

{% get_comment_form for comments_object as form %}
<form action="{% comment_form_target %}" method="POST">
[…]
{% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}
    {% for field in form.fields %}
        {{field}}
    {% endfor %}
[…]
</form>

The output looks like this:

    <form action="/comments/post/" method="POST"> 
            <input type="hidden" name="content_type" value="flatpages.flatpage" id="id_content_type" />         
            <input type="hidden" name="object_pk" value="1" id="id_object_pk" /> 
            <input type="hidden" name="timestamp" value="1269522506" id="id_timestamp" />              
            <input type="hidden" name="security_hash" value="ec4…0fd" id="id_security_hash" /> 
            content_type
            object_pk
            timestamp
            security_hash
            name
            email
            url
            comment
            honeypot
        […]
    </form> 
</div> 

Can you tell me what I'm doing wrong? Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

茶花眉 2024-09-02 11:45:57

use {% for field in form.visible_fields %}

form.fields 是一个字典,其中键是字段的名称,值是实际的 form.Field() 对象。

您还可以执行 {% for field in form %} ,其中应包含隐藏字段和可见字段。

use {% for field in form.visible_fields %}

form.fields is a dictionary where the keys are the names of the fields, and the values are the actual form.Field() objects.

You can also do {% for field in form %} which should include both hidden and visible fields.

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