Fred Collins
Fred Collins

Reputation: 5010

django-comments questions

I've just implemented django-comments.

settings.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 %}

templates/comments/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 %}

templates/comments/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 %}

When form is rendered I see this html code:

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> 

Questions:

Thanks for all.

Upvotes: 0

Views: 312

Answers (1)

markijbema
markijbema

Reputation: 4055

Line 4 seems ok? I see no problem at least ;) But of course always test your code :)

The removal is a bit hackish, but that is also because the templatinglanguage is a bit limited. If you have a recent version you can probably use the in operator though: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#in-operator

The hardcoding is not ok, use the url templatetag: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#url

You can do it with Ajax. I did that for the comment app using Dajax (a django app; http://www.dajaxproject.com ), and just calling the view from the dajax function, but you can call it from another function as well. My solution was letting Dajax render a html snippet, and just sending that back, and also use that snippet from the main template. That way the layout code was in one place (though not very network-usage-efficient).

Upvotes: 2

Related Questions