django - 如何自定义 Django 的评论表单?

标签 django django-forms django-comments

我正在尝试自定义 Django 的评论表单。内django.contrib.comments.forms我注意到所有的字段表单都在类 CommentDetailForm 中声明。 ,继承自 CommentSecurityForm .然后我想当我写模板标签时{% get_comment_form for order as form %} ,它正在获取名为 CommentForm 的类继承了 CommentDetailForm与蜜 jar 领域。

我想自定义评论表单,以便它只显示评论字段(而不是可选的名称、电子邮件或 URL 字段)。这些信息将由当前登录的用户提供。事实上,只有登录用户的特定UserProfile.user_type ( UserProfileUser 的外键)允许评论。

有关如何实现这一目标的任何提示?看 Django 评论的源代码已经吓到我了,哈哈。

编辑:

到目前为止,评论模板的外观如下:

{% get_comment_form for order as form %}
    <form action = "{% comment_form_target %}" method = "post">
        {% csrf_token %}
        {{ form }}
        <input type = "submit" name = "submit" value = "Post">
    </form>

该网站看起来像这样



我要隐藏Name , Email address , 和 URL .

最佳答案

您应该能够在模板中完成所有这些:

{% ifequal User.profile.user_type "comment_type" %}
{% get_comment_form for order as form %}
  <form action="{% comment_form_target %}" method="post">
    {% csrf_token %}
    {% for field in form %}
    {% ifequal field.name "name" %}
        <input id="id_name" type="hidden" name="name" value="{{ user.username }}" />
    {% else %}{% ifequal field.name "email" %}
        <input type="hidden" name="email" value="{{ user.email }}" id="id_email" />
    {% else %}{{ field }}{% endifequal %}{% endifequal %}
    {% endfor %}    
        <input type="submit" name="submit" value="Post">
  </form>
{% endifequal %}

关于django - 如何自定义 Django 的评论表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9883093/

相关文章:

django-allauth 再次重定向到注册/登录表单

django - 模型自依赖(一对多字段)实现

javascript - 使用 JQUERY 立即将选中的复选框值从一个复选框复制到另一个复选框

javascript - 如何通过 javascript 使用 django 评论表单?

python - 如何使 django 评论模型字段不需要站点

django - WSGIRequest错误使用django_model_comments

django - 关闭 Django 中的迁移

python - Django 是否有等效的 Javadoc?

python - Django ModelForm的插入表单也显示已有记录

python - 将 Django 电子邮件打印到控制台时出现问题