[Fixed]-How to extend the comments framework (django) by removing unnecessary fields?

12đź‘Ť

âś…

I recently implemented the solution that Ofri mentioned, since I only wanted to accept a solitary “comment” field for a comment (like SO does, no “name”, no “email” and no “url”).

To customize the default comment form and list display, I created a “comments” directory in my root “templates” directory and overrode the two default comment templates.

My “/templates/comments/form.html” is:

{% load comments i18n %}
{% if user.is_authenticated %}
    <form action="{% comment_form_target %}" method="post">
        {% csrf_token %}
        {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
        {% for field in form %}
            {% if field.is_hidden %}
                {{ field }}
            {% else %}
                {% if field.name != "name" and field.name != "email" and field.name != "url" %}
                    {% if field.errors %}{{ field.errors }}{% endif %}
                    <p {% if field.errors %} class="error"{% endif %} {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
                    {{ field }}
                    </p>
                {% endif %}
            {% endif %}
        {% endfor %}
        <input type="submit" name="post" class="submit-post" value="{% trans "Add Comment" %}" />
    </form>
{% else %}
    I'm sorry, but you must be <a href="javascript:alert('send to login page')">logged in</a> to submit comments.
{% endif %}

Which is only slightly different from the default comments form, primarily suppressing the display of the not-required “name”, “email” and “url” inputs.

My “/templates/comments/list.html” is:

<div class="comment_start"></div>
{% for comment in comment_list %}
    <div class="comment">
       {{ comment.comment }} 
       (from <a href="javascript:alert('show user profile/stats')">{{ comment.user }}</a> - {{ comment.submit_date|timesince }} ago)
    </div>
{% endfor %}

On the page I want the form, I first call {% load comments %} and then {% render_comment_form for [object] %} to show the form, or {% render_comment_list for [object] %} to generate a list of the comments on the object (replace [object] with your appropriate object name).

This is working great for me, and still giving me all the other “free” stuff that comes with django comments (moderation, flagging, feeds, polymorphic associations, etc…)

👤mitchf

4đź‘Ť

A tidy summary of how to do this elegantly, through the actual comments framework subclassing approach, rather than hiding elements in a form/other untidy hacks, can be found Django Comments: Want to remove user URL, not expand the model. How to?

Essentially, you subclass the CommentForm, and change its get_comment_create_data(self) method, and then pop out the attributes you don’t want (e.g. email, url, etc.)

J

👤jvc26

2đź‘Ť

You can try overriding the comment form with a custom template that only shows the fields you want.

👤Ofri Raviv

Leave a comment