我正在尝试自定义 Django 的评论表单。内django.contrib.comments.forms
我注意到所有的字段表单都在类 CommentDetailForm
中声明。 ,继承自 CommentSecurityForm
.然后我想当我写模板标签时{% get_comment_form for order as form %}
,它正在获取名为 CommentForm
的类继承了 CommentDetailForm
与蜜 jar 领域。
我想自定义评论表单,以便它只显示评论字段(而不是可选的名称、电子邮件或 URL 字段)。这些信息将由当前登录的用户提供。事实上,只有登录用户的特定UserProfile.user_type
( UserProfile
有 User
的外键)允许评论。
有关如何实现这一目标的任何提示?看 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/