Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Disqus to Django blog for commenting

I'm new to python and so django. I built a simple blog with models including "Entry" and "Author". Now, I've been told that the blog will make use of "disqus" for comments and so I should give some identifier or site id at backend so that disqus can be used. I can not figure it out how to do it. Though, I saw its functionality by adding the universal code (provided by disqus) to my blog on Blogger.

my models are:

class Author(models.Model):
    userName = models.CharField(max_length=50)
    displayName=models.CharField(max_length=100)
    email = models.EmailField()

    def __str__(self):             
        return self.displayName

class Entry(models.Model):
    title = models.CharField(max_length=255)
    body = MarkdownField()
    image=models.ImageField(upload_to='images',null=True,blank=True)
    category=models.ManyToManyField(Category)
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)
    authors = models.ManyToManyField(Author)
    publish=models.BooleanField(default=True)

    def __str__(self):             
        return self.title

    class Meta:
        verbose_name = "Blog Entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-createdAt"]

my views are:

class EntryView(viewsets.ModelViewSet):
    queryset=Entry.objects.all()
    serializer_class=EntrySerializer
    lookup_field = 'title'
    paginate_by= 6

class AuthorView(viewsets.ModelViewSet):
    queryset=Author.objects.all()
    serializer_class=AuthorSerializer

how can I add disqus to my blog?

like image 694
l0neW0lf_0178 Avatar asked Mar 15 '23 19:03

l0neW0lf_0178


2 Answers

After you register on disqus.com and enter your web site url (https://www.example.com/) and your website shortname as examplename.

You need to change these three things, canonical url for the page on which you want to load the comments, page identifier and shortname.

Identifier needs to be unique for every page, so it can be random number or slug of the article link as you can see in the example bellow.

Set something like this using your own values:

this.page.url = 'https://www.example.com/blog/{{ article.url }}/';
this.page.identifier ='/{{ article.url }}/';
s.src = '//examplename.disqus.com/embed.js';

Then you need to replace these three lines in the code bellow with your own and add that code where you want your Disqus to load:

<div id="disqus_thread"></div>
<script>

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://www.example.com/blog/{{ article.url }}/';
this.page.identifier ='/{{ article.url }}/';
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//examplename.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
like image 80
sp_omer Avatar answered Mar 17 '23 09:03

sp_omer


Here is an example of Disqus integration for django-anda blog.

Add the following HTML to your template where you want the comments appear. Please note that Disqus uses URL to map comments to posts, so you must use fixed URLs or read Disqus API documentation how to explicitly bypass page identifier to Disqus.

<div id="disqus_thread"></div>

<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = 'YOURIDGOESHERE';

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
like image 43
Mikko Ohtamaa Avatar answered Mar 17 '23 09:03

Mikko Ohtamaa