Ticket #12871: 12871.diff

File 12871.diff, 2.5 KB (added by Tim Graham, 6 years ago)

Updated the patch a bit

  • docs/ref/contrib/comments/index.txt

    diff --git a/docs/ref/contrib/comments/index.txt b/docs/ref/contrib/comments/index.txt
    index af937e0..3eede2d 100644
    a b you can include a hidden form input called ``next`` in your comment form. For ex 
    253253    <input type="hidden" name="next" value="{% url 'my_comment_was_posted' %}" />
     255Providing a comment form for authenticated users
     258If a user is already authenticated, it makes little sense to display name,
     259email, and URL fields, since these can already be retrieved from their login
     260data and profile. In addition, some sites will only accept comments from
     261authenticated users.
     263To provide a comment form for authenticated users, you can manually provide the
     264additional fields expected by the Django comments framework. For example,
     265assuming comments are attached to the model "item"::
     267    {% if user.is_authenticated %}
     268        {% get_comment_form for item as form %}
     269        <form action="{% comment_form_target %}" method="POST">
     270        {% csrf_token %}
     271        {{ form.comment }}
     272        {{ form.honeypot }}
     273        {{ form.content_type }}
     274        {{ form.object_pk }}
     275        {{ form.timestamp }}
     276        {{ form.security_hash }}
     277        <input type="hidden" name="next" value="{% url item_view item.id %}" />
     278        <input type="submit" value="Add comment" id="id_submit" />
     279        </form>
     280    {% else %}
     281        <p>Please <a href="{% url auth_login %}">log in</a> to leave a comment.</p>
     282    {% endif %}
     284The honeypot, content_type, object_pk, timestamp, and security_hash fields are
     285fields that would have been created automatically if you had simply used
     286``{{ form }}`` in your template, and are referred to in `Notes on the comment
     287form`_ below.
     289Note that we do not need to specify the user to be associated with comments
     290submitted by authenticated users. This is possible because the :doc:`Built-in
     291Comment Models</ref/contrib/comments/models>` that come with Django associate
     292comments with authenticated users by default.
     294In this example, the honeypot field will still be visible to the user; you'll
     295need to hide that field in your CSS::
     297    #id_honeypot {
     298        visibility: hidden;
     299    }
     301If you want to accept either anonymous or authenticated comments, replace the
     302contents of the "else" clause above with a standard comment form and the right
     303thing will happen whether a user is logged in or not.
    255305.. _notes-on-the-comment-form:
    257307Notes on the comment form
Back to Top