我刚刚实现了 django-comments。
设置.py
INSTALLED_APPS = (
...
'django.contrib.comments',
)
product_detail.html
{% get_comment_count for product as comment_count %}
<p>This event has {{ comment_count }} comments.</p>
{% render_comment_list for product %}
{% render_comment_form for product %}
模板/评论/form.html
{% load comments i18n %}
{% if user.is_authenticated %}
<form action="{% comment_form_target %}" method="post">
{% csrf_token %}
<input type="hidden" name="next" value="/product/{{ product.id }}/" />
{% 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 %}
模板/评论/list.html
<div class="comment_start"></div>
{% for comment in comment_list reversed %}
<div class="comment">
{{ comment.comment }}
(from <a href="/user/{{ comment.user }}/">{{ comment.user }}</a> - {{ comment.submit_date|timesince }} ago)
</div>
{% endfor %}
呈现表单时,我看到了这个 html 代码:
1 <form action="/comments/post/" method="post">
2 <div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='39cad78f1b4adef30adb536717cesd71' /></div>
3 <input type="hidden" name="next" value="/product/1/" />
4 <input type="hidden" name="content_type" value="myapp.product" id="id_content_type" />
5 <input type="hidden" name="object_pk" value="2" id="id_object_pk" />
6 <input type="hidden" name="timestamp" value="1310776114" id="id_timestamp" />
7 <input type="hidden" name="security_hash" value="34efe5f91239db95f429d07ec21a2926bf22a905b65" id="id_security_hash" />
8 <p><textarea id="id_comment" rows="10" cols="40" name="comment"></textarea></p>
9 <p style="display:none;">
10 <input type="text" name="honeypot" id="id_honeypot" />
11 </p>
12 <input type="submit" name="post" class="submit-post" value="Add Comment" />
13 </form>
问题:
- 见第 4 行。没关系,有那个值吗?
- 这是从表单中删除名字、姓氏和 url 字段的好方法(覆盖 form.html)吗?
- 这个硬编码可以吗?
value="/product/{{ product.id }}/
- 我会用 ajax/jquery 插入评论,而不是完全刷新页面,这可能吗?
谢谢大家。