Sunday, March 12, 2006

Building a blog, part 5

To enable comments function in the blog is suppose to be pretty easy, as Django come pre-built with a undocumented comments system.

First step, add this line to,

(r'^comments/', include('django.contrib.comments.urls.comments')),

Problem is we do have to files, one is in django\bin\myblog, and another in django\bin\myblog\blog. By comparing existing codes in these, I think this should refer to the within the blog folder, so now my blog\ look like this...

from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^blog/', include('')),
((r'^admin/'), include('django.contrib.admin.urls.admin')),
(r'^comments/', include('django.contrib.comments.urls.comments')),
Next will be adding 'django.contrib.comments' to the at INSTALLED_APPS. Then run python install comments to install it.

This actually tell Django urls parser to look for url configuration file in django/contrib/comments/urls/ whenever there is a request for commetns/*.
Now run python install comments to install it.
To utilise the comments function, create a folder called comments under mytemplate,

In the "comments" folder, create two templates,
  • free_preview.html - to preview the comments being made.
  • posted.html - to confirm comments being posted and thanks the contributor.


{% extends "base" %}
{% block content %}
<h1>Preview your comment</h1>
<form action="../postfree/" method="post">
{% if comment_form.has_errors %}
<p><strong style="color: red;">Please correct the following errors.</strong></p>
{% else %}
<div class="comment">

{{ comment.comment|escape|urlizetrunc:"40"|linebreaks }}
<p>Posted by <strong>{{ comment.person_name }}</strong></p>

<p><input type="submit" name="post" value="Post public comment" /></p>

<h1>Or edit it again</h1>
{% endif %}

{% if comment_form.person_name.errors %}
{{ comment_form.person_name.html_error_list }}
{% endif %}
<p><label for="id_person_name">Your name:</label> {{ comment_form.person_name }}</p>

{% if comment_form.comment.errors %}
{{ comment_form.comment.html_error_list }}
{% endif %}

<p><label for="id_person_name">Comment:</label><br />{{ comment_form.comment }}</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="Preview revised comment" /></p>


{% endblock %}

{% extends "base" %}

{% block content %}

<h1>Comment posted successfully</h1>

<p>Thanks for contributing.</p>

{% if object %}
<li><a href="{{ object.get_absolute_url }}">View your comment</a></li>


{% endif %}

{% endblock %}

Now we have to enable user to add comments to a particular post, to do so, amend the posts_detail.html by adding this "underneath the detail of the blog post in the template (eg under the block that displays the tags used for this post, but before the end of the block)".

{% load comments %}

{% get_free_comment_list for blog.posts object.slug as comment_list %}
<h2 id="comments">Comments</h2>
{% for comment in comment_list %}
<div class="comment" id="c{{ }}">
<p><b>{{ comment.person_name }}</b> commented, on {{ comment.submit_date|date:"F j, Y" }} at {{ comment.submit_date|date:"P" }}:</p>

{{ comment.comment|escape|urlizetrunc:40|linebreaks }}
{% endfor %}

<h2>Post a comment</h2>
{% free_comment_form for blog.posts object.slug %}
{% endblock %}

Well, I am finding it rather difficult to understand where exactly this place is refering to, so I just trial and error really and paste it here,

It work, now if I click on a particular post, I get to see the comments made as well as post comments form, but the problem is comments made for a particular post will be display even if you view any other post! Hmmm... That must be something wrong with my code then, maybe I have add this code wrongly in posts_details.html? I am not sure, just need to try again...


Yannick said...

Seems like you are on a roll. Looks like you are learning quite a bit. Keep at it kahfei.

kahfei said...

Hey Yannick, again, thanks for the good words.
I hope I could speed thing up though, but we all have other things to do, family, works, etc.

Yannick said...

Definitely kahfei, I haven't been able to do much of anything lately due to other things keeping me busy. So I understand.

kahfei said...

How I miss the time in college, where I could and properly should use those time better....

Anonymous said...

Hi there, I create your blog via Google while searching throughout gold medal grant-in-aid on the side of a callousness bout and your direct looks damned thought-provoking on me