How to extend the comments framework (django) by removing unnecessary fields?

前端 未结 3 1059
忘掉有多难
忘掉有多难 2020-12-24 15:15

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,

3条回答
  •  滥情空心
    2020-12-24 15:37

    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 %}
        
    {% csrf_token %} {% if 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 %} {% endif %} {% endif %} {% endfor %}
    {% else %} I'm sorry, but you must be logged in 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:

    {% for comment in comment_list %}
    {{ comment.comment }} (from {{ comment.user }} - {{ comment.submit_date|timesince }} ago)
    {% 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...)

提交回复
热议问题