Django - Disqus无法识别唯一标识符

chi*_*rew 9 python django disqus

基本上,每个帖子都会出现同样的评论.我已经读过为什么会发生这种情况,但仍然无法弄清楚出了什么问题.

这是我在页面上看到的内容: 1

这是我的模板代码:

{% block content %}
    <p> The post id is: {{ post_object.id}} </p>
    <p> The post URL: {{ post_object.get_absolute_url }}


    {# DISQUS #}
    <div id="disqus_thread"></div>
    <script type="text/javascript">
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'MySiteName'; // required
    var disqus_identifier = '{{ post_object.id }}';
    var disqus_url = 'http://localhost:8000{{ post_object.get_absolute_url }}';
    var disqus_title = '{{ post_object.title }}';
    var disqus_developer = 1;        

/* * * 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="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)

呈现的HTML:

<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'MySiteName'; // required
var disqus_identifier = '42';
var disqus_url = 'http://localhost:8000/post/42/';
var disqus_title = 'Test post';
var disqus_developer = 1;

/* * * 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="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
Run Code Online (Sandbox Code Playgroud)

如您所见,disqus_identifier和disqus_url是唯一的.这里发生了什么?

任何想法或反馈都有帮助!谢谢!


编辑:好的,我看到问题来自哪里.在对帖子发表评论后发帖说http://localhost:8000/post/42/,Disqus添加到Disqus管理员(在"讨论标签"下)指向该帖子的链接http://localhost:8000/post

这甚至不是我页面上的有效网址.当我明确地将链接更改为时http://localhost:8000/post/42/,它会保存.但是,新创建的帖子仍会显示帖子42中的评论.

思考?

use*_*284 5

安装django-disqus并在模板中使用它.

pip install django-disqus
Run Code Online (Sandbox Code Playgroud)

将disqus添加到您的INSTALLED_APPS并将disqus api密钥放入您的设置中:

settings.py

INSTALLED_APPS = (
    ...
    'disqus',
    ...
)

DISQUS_API_KEY = 'YOUR_SECRET_API_KEY'
DISQUS_WEBSITE_SHORTNAME = 'YOUR_WEBSITE_SHORTNAME'
Run Code Online (Sandbox Code Playgroud)

在模板中使用disqus模板标签:

some_template.html

# load the tags
{% load disqus_tags %}
# get comments for your website
{% disqus_show_comments "YOUR_WEBSITE_SHORTNAME" %}
# get the url for the current object to get the right comments
{% set_disqus_url object.get_absolute_url %}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


小智 0

相反,您可以尝试使用django-disqus之类的东西,它使用简单的模板标签来加载 disqus 评论。所需要的只是:

# for when using the development server 

{% load disqus_tags %}
{% disqus_dev %}

# for showing all comments of a thread in production

{% load disqus_tags %}
{% disqus_show_comments %}
Run Code Online (Sandbox Code Playgroud)