質問

I am trying to customize Django's comments form. Inside django.contrib.comments.forms I noticed that all the field forms are declared in the class CommentDetailForm, which is inherited from CommentSecurityForm. Then I think when I write the template tag {% get_comment_form for order as form %}, it's getting the class called CommentForm which inherits CommentDetailForm with a honeypot field.

I wanted to customize the comments form so that it only displays the comments field (and not the optional name, email, or URL fields). Those information will be given by the current logged in user. In fact, only logged in users with certain UserProfile.user_type (UserProfile has a foreign key to User) are allow to comment.

Any tips on how to achieve this? Looking at the source code of the Django's comments already scares me lol.

EDIT:

Here is how the comment template looks so far:

{% 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>

And the site looks like this

I want to hide Name, Email address, and URL.

役に立ちましたか?

解決

You should be able to do all of this in the template:

{% 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 %}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top