0
votes

I have a django blog app and am trying to put disqus on each of my pages with a single blog entry but all comments show up on all entries. I've looked at related questions and think my problem might be with get_absolute_url.

So originally I had this model and get_absolute_url set:

class posts(models.Model):
    author = models.CharField(max_length = 30)
    title = models.CharField(max_length = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()
    slug = models.SlugField(max_length = 255)
    description = models.TextField()

    def __unicode__(self):
        return self.bodytext

    def get_absolute_url(self):
        return reverse('blog.views.home')

This was simply returning a '/' as my get_absolute_url, and when I tried adding args it just returned an empty string...so I tried experimenting with:

 def get_absolute_url(self):
        return reverse('blog.views.single', args=[self.slug])

and

def get_absolute_url(self):
        return reverse('single', args=[self.slug])

Which in the source code the Javascript disqus-url has something like 'single/secondentry' but the comments are still appearing on all of the entries.

So I think part of the problem might be that I simply don't understand what get_absolute_url is supposed to be. After googling and reading the docs I thought it would be the url to my homepage('blog.views.home') but when I set it as that, if I add any arguments it just returns an empty string.

Here are my urls for home and single pages:

 url(r'^$', 'blog.views.home', name='home'),
    url(r'^$', 'blog.views.index', name='index'),
    url(r'^single/(?P<slug>[\w-]+)/$', 'blog.views.single', name='single'),
    ...

For the immediate problem here, the disqus Javascript elements seem to be getting assigned correctly otherwise, so I'm not sure why the comments are still showing up on all of the entries.

var disqus_shortname = 'sitename';
var disqus_developer = "1";
var disqus_identifier = "1";
var disqus_url = "/single/first/";
var disqus_title = "First post";
1
Since you have a URL name, you can use that: return reverse('single', args=[self.slug]) . Also, do you have a namespace specified for blogs in Root URL conf ?karthikr
I tried that and got the same result/javascript variable, but the comments are still showing up on all entries.LVNGD
can you edit your question with how you are using it ?karthikr
Sure, I added the named url, but I think that get_absolute_url is returning what i want because I see it in the javascript variable, I just can't figure out why the disqus comments are still appearing on all of the entries instead of only the entry they are posted.LVNGD
@karthikr: I think its because he's using a named argument. Thus he needs to use kwargs.Games Brainiac

1 Answers

0
votes

I believe that the problem is that you have a named argument, (?P<slug>[\w-]+). Thus instead of args, you need to use kwargs.

So something like:

def get_absolute_url(self):
    return reverse('blog.views.single', kwargs={'slug': self.slug})

I believe that this should solve your problem.