0

我刚刚实现了 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 插入评论,而不是完全刷新页面,这可能吗?

谢谢大家。

4

1 回答 1

2

4号线好像没问题?至少我认为没有问题 ;) 但当然总是测试你的代码 :)

删除有点骇人听闻,但这也是因为模板语言有点有限。如果您有最新版本,您可能可以使用 in 运算符:https ://docs.djangoproject.com/en/dev/ref/templates/builtins/#in-operator

硬编码不行,使用url模板标签: https ://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#url

你可以用 Ajax 做到这一点。我使用 Dajax(一个 django 应用程序;http: //www.dajaxproject.com )为评论应用程序做到了这一点,并且只是从 dajax 函数调用视图,但您也可以从另一个函数调用它。我的解决方案是让 Dajax 呈现一个 html 片段,然后将其发送回来,并使用主模板中的那个片段。这样布局代码就在一个地方(尽管网络使用效率不是很高)。

于 2011-07-16T00:52:23.370 回答