14

I've been reading on the django docs about the comments framework and how to customize it (http://docs.djangoproject.com/en/1.1/ref/contrib/comments/custom/) In that page, it shows how to add new fields to a form. But what I want to do is to remove unnecesary fields, like URL, email (amongst other minor mods.)

On that same doc page it says the way to go is to extend my custom comments class from BaseCommentAbstractModel, but that's pretty much it, I've come so far and now I'm at a loss. I couldn't find anything on this specific aspect.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ignacio
  • 7,947
  • 15
  • 63
  • 74
  • Why do you need to remove them? If you take them out, won't you have problems with any pluggable app that uses the comments framework? They're not really doing any harm and the amount of disk space they consume is negligible. – Tom Mar 06 '10 at 16:50
  • 1
    I don't mean remove them like I want to go and hack the comments code. I just want to omit them in this specific app. Disclaimer: I'm hoping there is a nicer way than hiding it with css and assign those fields a null values behind the scenes, so refrain from posting that answer, unless it's to tell me it's the only way to do it. :) – Ignacio Mar 06 '10 at 17:10

3 Answers3

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
  • 3,697
  • 4
  • 26
  • 29
  • 1
    It's been a while, but this is exactly what I needed back then. Thanks! – Ignacio Aug 06 '10 at 19:06
  • How do you tell the comments app to use your custom templates? – john2x Feb 21 '11 at 19:11
  • Just put your own templates named "form.html" and "list.html" in a directory called "comments" in your templates directory. – mitchf Feb 22 '11 at 17:13
  • This didn't work for me in Django 1.4 because the 'name' and 'email' fields are required by the built-in form class. – Jonathan Berger Feb 14 '13 at 00:19
  • 1
    @JonathanBerger: you can [customize the form](https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/). Also see [this answer](http://stackoverflow.com/questions/9947343/a-user-with-no-email-cant-post-a-comment-using-djangos-comments-framework?rq=1). – Sergey Orshanskiy Dec 12 '13 at 03:43
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

Community
  • 1
  • 1
jvc26
  • 6,363
  • 6
  • 46
  • 75
2

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

Ofri Raviv
  • 24,375
  • 3
  • 55
  • 55
  • Ofri, if you read my comment on the question, you'll see that this is something I want to avoid, at least ideally. Thanks anyway. – Ignacio Mar 08 '10 at 11:40
  • I did read your comment, and I'm not suggesting you hide the fields with CSS. You will not render the fields in your template, and thus they will never exist. Its a clean solution, and everybody use it as far as i know. You're welcome to invent a new wheel, if you like. – Ofri Raviv Mar 08 '10 at 15:00
  • My bad! So, the answer might have been there the whole time. So, if I just don't render this fields they just won't be required and the framework will ignore them? – Ignacio Mar 08 '10 at 17:08
  • I think they're not required anyway, not *because* you don't show them. Note that you need the comment to be made by an authenticated user, OR have user_name. it won't work without both of them. if you want completely anonymous comments, you will have define your own comments model. – Ofri Raviv Mar 08 '10 at 20:08
  • Thanks for the help. Anyway, this took too much time for such a simple functionality that I need. I'll just create a Comment model and keep moving on. – Ignacio Mar 09 '10 at 02:51