Taking the ugly out of Django comments

Posted by: barbara | Date: Nov 07, 2008 | Category: django
Django comments are pretty easy to implement - most of what you need to get access to the app and template tags is covered in the documentation:

http://docs.djangoproject.com/en/dev/ref/contrib/comments/

Regarding the template tags - you should be able to figure out what properties the comment object has by looking at the django_objects table in your database.
    {% load comments %}

    {% get_comment_list for entry as cmt_list %}
    {% for comment in cmt_list %}
    <div class="comment">
        <p><a href="{{ comment.user_url }}">{{ comment.user_name }}</a> - {{ comment.submit_date }}</p>
        {{ comment.comment }}
    </div>
    {% endfor %}

    <div class="comment_form">
        {% render_comment_form for entry %}
    </div>
Here's a helpful article for reference:

django comment fields [Revolution blahg]

But then the default preview and confirmation templates are sort of ugly:





To customize them a little, at least to have them extending your own base template so that they match the look and feel of your site, copy a few of the default templates out of Django:
    django/contrib/comments/templates/comments/preview.html
    django/contrib/comments/templates/comments/posted.html
into a '/comments/' folder in one of your project's template directories:
    belleville/templates/comments/preview.html
    belleville/templates/comments/posted.html
First things first, do a little cleanup on those templates now living in your project - make sure they're extending from your own base template, that block tags are using your tag names, etc.:
    {% extends "base.html" %}
    {% block page_title %}
        Preview your comment
    {% endblock %}
Now, one thing the default confirmation template doesn't have is a link back to the original entry. But I really don't want to make users go all the way back to the beginning to find their posted comments. A timed redirect might be slick, but I'll think about that later.

For now, I'm just adding a link to the template - using the one property of the comment object that I know has a relationship with the original post: the post object's primary key.
    {% block content %}
        <h1>Thank you for your comment.</h1>
        <a href="/viewid/{{ comment.object_pk }}/">Return to the original post</a>
    {% endblock %}
If you've looked at any of the other entries on this blog, you've seen that I use '/view/' with a slug as the pattern to view individual posts. But I don't have the slug here, so instead I'm using the pk and doing a quick redirect through another method, viewid.

Here's the pattern in the urls.py:
    url(r'^viewid/(?P<post_id>\w+)/*$',            views.viewid,           name='entry_viewbyid'),
No great mystery there. And here's the view method:
    def viewid(request, post_id):
        """
        """
        try:
            commented_entry = Post.objects.get(pk=post_id, publish=True)
            title = commented_entry.slug
            return HttpResponseRedirect('/view/%s/' % title)
        except ObjectDoesNotExist:
            return HttpResponseRedirect('/')
So when the pk value is passed in, we can get the post object and its slug, then redirect to the normal view. If we don't have the pk, there's nothing we can do but return the user to the blog's main page.

Ryan Berg - 2008-11-07 03:00:23

Don't forget that each comment has a get_content_object_url() method which will point to the content object (obviously), and get_absolute_url() method which will point to the comment's anchor on the content object page, like so: /path/to/object/#cCOMMENT_ID

These can be used in posted.html {{ comment.get_absolute_url }} to link without having to write a redirect view.

tony - 2008-11-07 03:34:27

thx

Mark M - 2008-12-07 03:43:34

Hi Barbara. This post has been useful in filling in some of the gaps in the django docs. Thanks.

Regarding the redirect, after looking through the django code I noticed you can add a hidden 'next' field to the form with the url to which you'd like the user to return after they submit their comment in order to bypass the posted.html template.

pytune - 2009-01-18 11:57:44

Huh, nice method. I hope you will make "primary" redirect later and discribe it on your blog.

P.S. Nice to girl in django :)

Ryan - 2009-02-03 20:51:29

I was wondering how to clean up the "you goofed/validation" page. now I know. After spending hours figuring out how to implement reCaptcha into my current project, I was completely let down by the look and feel of the "redo this form" page. (I'd never intentionally messed it up to see that it was "ugly" and by then, I ran out of gumption to clean it up.

Jason - 2009-04-21 12:25:20

Thanks for the post. I wasn't sure what the best method was for spicing up the bare bones comments templates, and other built-in Django app templates. So the answer is to copy the templates to your project and roll your own urls and views. Makes sense!

pyguy - 2009-04-26 04:21:02

thanks for this, probably simple to old django hands, but an important tip to using django comments that I think should be in the documentation.

Puneet - 2009-07-21 17:20:24

Thanks for the nice explanation

Andrew - 2009-07-23 18:48:11

So, nice! Thank u!:)

Skylar - 2009-08-29 10:01:00

I just have to give it a try.

insurance - 2010-01-17 13:01:27

yea nice article.I got useful information.Thanks for sharing us a great information article ..that is actually helpful.Regards,

ppo plans - 2010-01-26 07:52:44

thanks for sharing this useful information with us.

Ron - 2010-02-09 08:50:11

hello, im having a hard time figuring out how to customize the comment fields generated by
<div class="comment_form">
{% render_comment_form for entry %}
</div>
as you can see by default Name, Email Address, URL and Comment field are not properly aligned, can anyone here help me with this? i dont know what id's to access in my css. I know this is not a forum but it's somewhat related.

Online Sweepstakes - 2010-02-17 09:52:55

Thanks for the great blog — keep up the good work!

Igor Ganapolsky - 2010-02-25 00:05:51

Just an addendum to Barbara's main post:
in order to show a link to the original entry from the comment confirmation page (posted.html) it isn't necessary to reference the comment's primary key, create a new url mapping, or create a new view method. In my own project I simply put a link in my posted.html, that takes the user back to the original entry:
<A HREF="http://[... whatever your url ...]{{ comment.content_object.get_absolute_url }}">Return to entry</a>

17 und 4 Turniere - 2010-03-18 07:23:42

Thanks for the addition! Can you share some more information on what the significance of this is? What issue do we avoid or benefit do we gain by making sure ajax is first?
http://www.black-jack-regeln.de/

Debjit - 2010-03-25 21:20:59

Hi,

Thanks for the informative post. I have been trying to figure out a way to display the original post body on the preview page.

Is it possible to access the associated post before the comment is posted?

ccccccccccccccc - 2010-05-03 02:35:25

xxxxxxxxxxx

phone - 2010-05-11 09:47:42

Taking the ugly out of Django comments - nice topic, I searched it for some time, but I finally found it :)

Jessica Jameson - I am a Newbie Internet Marketer :)

Noah Spahn - 2010-06-25 04:56:09

What a treat to find this: Thank you!

testing - 2010-07-11 23:45:56

ty for this thread

andres - 2010-07-11 23:46:42

awdawd

karta kredytowa makro - 2010-08-16 16:36:17

Kredyty bez zgody wsp??ma??onka
Przedstawiamy Pa?stwu oferty kredyt?w got?wkowych, dla kt?rych nie jest wymagana zgoda wsp??ma??onka. Kredyty s? przeznaczone r?wnie? dla os?b z nie najlepsz? histori? w BIK. Procedury szybkie i uproszczone.
Kredyty bez zgody wsp??ma??onka maj? istotne znaczenie dla os?b znajduj?cych si? w trudnej sytuacji rodzinnej, nie zamieszkuj?cych wsp?lnie, b?d?cych w separacji lub w trakcie rozwodu i nie maj?cych rozdzielno?ci maj?tkowej.
W takiej sytuacji tylko kilka bank?w jest w stanie udzieli? po?yczki.
EUROBANK na dow?d i bez za?wiadcze? o zarobkach oraz bez BIK i zgody wsp??ma??onka mo?e udzieli? nawet 30 tys z? na 7-8 lat !!! Wystarczy z?o?y? wniosek.
W pozosta?ych bankach mog? Pa?stwo uzyska? od 10-20 tys z? bez zgody wsp??ma??onka.