1

I have a working blog. Each post has a link "Comments" that leads to comments for that particular post. I want to add comment count so it would look like: "Comments (5)" ect.

I added % get_comment_count for post.pk as comment_count %} value to my template, so it looks like this :

    {% for post in posts.object_list %}
<div class="post">
    <div class="title">{{ post.title }}</div>
    <ul>
        <div class="time">{{ post.created }}</div>
        <div class="body tresc">{{ post.body }}</div>
        {% get_comment_count for post.pk as comment_count %}
        <div class="commentlink"><a href="post/{{ post.pk }}/">{{ comment_count }} Comments</a></div>

    </ul>
 </div>
{% endfor %}

but I get the error :

Invalid block tag: 'get_comment_count', expected 'empty' or 'endfor'

at line containing {% get_comment_count for post.pk as comment_count %}. What am I doing wrong?

My models.py looks like that :

from django.db import models
from django.contrib import admin

class Post(models.Model):
    title = models.CharField(max_length=60)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.title

class PostAdmin(admin.ModelAdmin):
    search_fields = ["title"]

class Comment(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=60)
    body = models.TextField()
    post = models.ForeignKey(Post)

    def __unicode__(self):
        return unicode("%s: %s" % (self.post, self.body[:60]))

class CommentAdmin(admin.ModelAdmin):
    display_fields = ["post", "author", "created"]

admin.site.register(Comment, CommentAdmin)
admin.site.register(Post, PostAdmin)

How can I use db_table to change the table Django is reading comments from? I need it to read from news_comment table.

3
  • You're trying to use a list comprehension in django templates, and list comprehensions don't work in templates. Dec 1, 2013 at 12:57
  • This has nothing to do with list comprehensions, it seems like the tag is not importable (or conflicts with for loop), can you try calling get_comment_count outside the for loop ? Also make sure you have {% load comments %} at the top of your template.
    – jbub
    Dec 1, 2013 at 13:07
  • Ok it worked, but I get : "'int' object has no attribute '_meta'". Am I looping for something wrong or?
    – Fengson
    Dec 1, 2013 at 13:29

1 Answer 1

0

First make sure you load the comments template tags at the top of your template:

{% load comments %}

The get_comment_count expects model instance as a parameter after for keyword. You are passing it an integer primary key value. So you need to update it like this:

{% get_comment_count for post as comment_count %}
9
  • I got "no such table: django_comments" error. I do have a table comment in "news" app so when I open SQLite Manager in Firefox Plugin I see : "news_comment" table with many fields, such as id, body, post_id, ect. How to link it with this table?
    – Fengson
    Dec 1, 2013 at 14:10
  • Make sure you have 'django.contrib.comments' in INSTALLED_APPS and then run manage.py syncdb.
    – jbub
    Dec 1, 2013 at 14:14
  • I forgot to do syncdb :) One more thing - how can I tell Django to use different table? I have all my posts added to one named "news_comment" so the new one is empty. Can I do it somehow?
    – Fengson
    Dec 1, 2013 at 14:19
  • You can use db_table option on your model Meta options class. See docs on that. If you want to customize the comments model see this link.
    – jbub
    Dec 1, 2013 at 14:23
  • Please, look at my initial post. I added my models.py Where should I put this code in order for Django to read from "news_comment" table instead of "django_comments" ?
    – Fengson
    Dec 1, 2013 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.