Python django评论问题

Python django评论问题,python,django,django-comments,django-contrib,Python,Django,Django Comments,Django Contrib,我刚刚实现了django注释 设置.py INSTALLED_APPS = ( ... 'django.contrib.comments', ) 产品详细信息.html {% get_comment_count for product as comment_count %} <p>This event has {{ comment_count }} comments.</p> {% render_comment_list for product %}

我刚刚实现了django注释

设置.py

INSTALLED_APPS = (
    ...
    'django.contrib.comments',
)
产品详细信息.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 %}
{% 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 %}
<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 %}
{%get\u comment\u count作为comment\u count%}
此事件有{comment\u count}}个注释

{%render_comment_list for product%} {%render\u comment\u form for product%}
模板/comments/form.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 %}
{% 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 %}
<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 %}
{%load comments i18n%}
{%if user.u经过身份验证%}
{%csrf_令牌%}
{%形式的字段为%}
{%if field.is_hidden%}
{{field}}
{%else%}
{%if field.name!=“name”和field.name!=“email”和field.name!=“url”%}
{%if-field.errors%}{{field.errors}{%endif%}

{{field}}

{%endif%} {%endif%} {%endfor%} {%else%} 很抱歉,你必须提交评论。 {%endif%}
模板/comments/list.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 %}
{% 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 %}
<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 %}

{%用于注释列表中的注释%}
{{comment.comment}}
(从-{comment.submit|date|timesince}开始)
{%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> 
1
2.
3.
4.
5.
6.
7.
8

9

10 11

12 13
问题:

  • 见第4行。没关系,有这个值吗
  • 这是从表单中删除名字、姓氏和url字段的好方法(覆盖form.html)吗
  • 这个硬代码可以吗
    value=“/product/{{product.id}}/
  • 我会用ajax/jquery插入注释,而不是用页面完全刷新,可以吗

谢谢大家。

第4行似乎还可以?我至少看不出有问题;)但当然要经常测试代码:)

删除有点黑客,但这也是因为templatinglanguage有点有限。如果您有最新版本,您可能可以使用in运算符:

硬编码不正常,请使用url templatetag:


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

但是在第4行中,站点的访问者可以看到appname和模型名,这正常吗?评论功能需要它,所以我想是的。我同意你的看法,虽然它不是很漂亮,但它有一点是因为评论应用的可插拔性。否则你怎么能构建一个可以对任何对象发表评论的通用视图呢?(一个可能的答案是将机密字符串映射到appname modelname的系统范围哈希,以及视图的包装器)。我已经修复了硬编码。现在,当我单击“提交注释”时“在文本区域中插入零个字符后,它会向我显示页面
/comment/post
,其中包含已完成的内容和错误。我可以在同一页面中显示错误,而不是在另一页面中显示错误吗?(我认为这很难看)显然不是:(我同意这很难看;))我认为可以对评论应用程序进行“子类化”,并添加/更改一些功能。在链接的帖子中,回答者还链接到他自己的帖子,在帖子中他写了一些应该能解决问题的东西。没问题:)快乐编码:)