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 urls.py,


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


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


from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^blog/', include('myblog.blog.urls')),
((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 settings.py at INSTALLED_APPS. Then run python manage.py install comments to install it.


This actually tell Django urls parser to look for url configuration file in django/contrib/comments/urls/comments.py whenever there is a request for commetns/*.
Now run python manage.py 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.

free_preview.html

{% 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>
</div>

<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>

</form>

{% endblock %}



Posted.html
{% extends "base" %}

{% block content %}

<h1>Comment posted successfully</h1>

<p>Thanks for contributing.</p>

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

</ul>

{% 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{{ comment.id }}">
<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 }}
</div>
{% 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...

5 comments:

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