| | 291 | |
| | 292 | Providing a comment form for authenticated users |
| | 293 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| | 294 | |
| | 295 | If a user is already authenticated, it makes little sense to display name, email, |
| | 296 | and URL fields, since these can already be retrieved from their login data and |
| | 297 | profile. In addition, some sites will only accept comments from authenticated users. |
| | 298 | |
| | 299 | To provide a comment form for authenticated users, build the form manually in your |
| | 300 | template as above, but manually provide the additional fields expected by the |
| | 301 | Django comments framework. For example, assuming comments are attached to the model |
| | 302 | "item":: |
| | 303 | |
| | 304 | {% if user.is_authenticated %} |
| | 305 | {% get_comment_form for item as form %} |
| | 306 | <form action="{% comment_form_target %}" method="POST"> |
| | 307 | {% csrf_token %} |
| | 308 | {{ form.comment }} |
| | 309 | {{ form.honeypot }} |
| | 310 | {{ form.content_type }} |
| | 311 | {{ form.object_pk }} |
| | 312 | {{ form.timestamp }} |
| | 313 | {{ form.security_hash }} |
| | 314 | <input type="hidden" name="next" value="{% url item_view item.id %}" /> |
| | 315 | <input type="submit" value="Add comment" id="id_submit" /> |
| | 316 | </form> |
| | 317 | {% else %} |
| | 318 | <p>Please <a href="{% url auth_login %}">log in</a> to leave a comment.</p> |
| | 319 | {% endif %} |
| | 320 | |
| | 321 | In this example, the honeypot field will still be visible to the user; you'll need |
| | 322 | to hide that field in your CSS:: |
| | 323 | |
| | 324 | #id_honeypot { |
| | 325 | visibility:hidden; |
| | 326 | } |
| | 327 | |
| | 328 | If you want to accept either anonymous or authenticated comments, replace the |
| | 329 | auth_login line above with a standard comment form, and the right thing will happen |
| | 330 | whether a user is logged in or not. |
| | 331 | |