基本上,每条帖子都会出现同样的评论。我已经读过为什么会发生这种情况,但仍然无法弄清楚出了什么问题。
以下是我在页面上看到的内容:
这是我的模板代码:
{% 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 %}
呈现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>
如您所见,disqus_identifier和disqus_url是唯一的。这是怎么回事?
任何想法或反馈都有帮助!谢谢!
http://localhost:8000/post/42/
的帖子发表评论后,Disqus会向“我的帖子”(在“讨论”标签下)添加帖子链接http://localhost:8000/post
这甚至不是我网页上的有效网址。当我明确地将链接更改为http://localhost:8000/post/42/
时,它会保存。但是,新创建的帖子仍会显示来自帖子42的评论。
思想?
答案 0 :(得分:5)
安装django-disqus并在模板中使用它。
pip install django-disqus
将disqus添加到您的INSTALLED_APPS并将disqus api密钥放入您的设置中:
settings.py
INSTALLED_APPS = (
...
'disqus',
...
)
DISQUS_API_KEY = 'YOUR_SECRET_API_KEY'
DISQUS_WEBSITE_SHORTNAME = 'YOUR_WEBSITE_SHORTNAME'
在模板中使用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 %}
希望这会有所帮助。
答案 1 :(得分: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 %}