Code


Version 6 (modified by Tyson Tate, 8 years ago) (diff)

Generic views don't need the import in views.py

Using the FreeComment application integrated to Django.

If you are using your own views and not Django's built-in generic views, you'll need to import FreeComment in views.py:

from django.contrib.comments.models import FreeComment

Edit your settings.py

INSTALLED_APPS = (
    [...]
    'django.contrib.comments',
)

In your html template you can use the comment system. Remember a comment is going to be link to an entry id , for any of your model, it could be a blog post or a recipe. Add this to your template file

{% load comments %}

to display the comments for the current entry wich is a recipe object r. recettes is the app name recipe the model in models.py

{% get_free_comment_list for recettes.recipe r.id as comment_list %}
{% if comment_list %}
    {% for comment in comment_list %}
         {{ comment.person_name }}
         {{ comment.submit_date|date:"j F, Y" }} 
         {{ comment.submit_date|date:"P" }}
         {{ comment.comment|escape|urlizetrunc:40|linebreaks }} 
    {% endfor %}
{% endif %}

Here is a larger example where you cycle throught 2 colors to display the comments.

{% get_free_comment_list for recettes.recipe r.id as comment_list %}
{% get_free_comment_count for recettes.recipe r.id as comment_count %}
{% if comment_list %}
	<h3>Comments ({{comment_count}})</h3>
	<br />
	{% for comment in comment_list %}
		<div class="{% cycle comment_even,comment_odd %}"  id="c{{forloop.counter}}">
		<div class="comment_content">
        		<span class="comnum"><a id="{{forloop.counter}}" href="#c{{forloop.counter}}">#{{forloop.counter}}</a></span>
                <p>by<b> {{ comment.person_name }}</b>, the{{ comment.submit_date|date:"j F, Y" }} at {{ comment.submit_date|date:"P" }}</p>
                {{ comment.comment|escape|urlizetrunc:40|linebreaks }} 
        </div>
        </div></div>
        </div>
	{% endfor %}
{% endif %}

We have to append a submit comment form to this.

Add a comment
{% free_comment_form for recettes.recipe r.id %} 

To use our own comments template, create a comments folder into your template folder location. In my case /home/greg/projects/cefinban/templates/comments There you could put your own freeform.html, free_preview.html, posted.html. freeform.html is located in /django/contrib/comments/template/comments

freeform.html

{% load i18n %}
{% if display_form %}
<form action="/comments/postfree/" method="post">
<p>{% trans "Your name:" %} <input type="text" id="id_person_name" name="person_name" /></p>
<p>{% trans "Comment:" %}<br /><textarea name="comment" id="id_comment" rows="10" cols="60"></textarea></p>
<input type="hidden" name="options" value="{{ options }}" />
<input type="hidden" name="target" value="{{ target }}" />
<input type="hidden" name="gonzo" value="{{ hash }}" />
<p><input type="submit" name="preview" value="{% trans "Preview comment" %}" /><input type="submit" name="post" value="{% trans "Submit" %}" /></p>
</form>
{% endif %}