【问题标题】:Why are comment permalinks for django-comments-xtd going to example.com in Wagtail?为什么 django-comments-xtd 的评论永久链接会转到 Wagtail 中的 example.com?
【发布时间】:2021-08-02 07:56:44
【问题描述】:

我正在使用 django-comments-xtd 在 Wagtail 中设置 cmets,而我无法弄清楚的一件事是为什么我的评论永久链接一直转到 https://example.com/news/bloggy-test-post/#c1 而不是 https://localhost:8000/news/bloggy- test-post/#c1.

我在 Wagtail Google 小组中发现了一个有类似问题的用户,他发现他们在 Wagtail 管理员中将“example.com”设置为他们的网站。但是我的设置中只有一个站点,它当前设置为“localhost”,端口为 8000。

Screenshot of the Wagtail admin site showing there is one site set to localhost with a port of 8000

我在所有文件和库中搜索“example.com”,发现的唯一其他设置是 base.py 中的 BASE_URL 设置。我尝试将其更改为 http://localhost:8000 并且评论永久链接仍被定向到“example.com”。

我还缺少其他设置吗?或者我应该以其他方式获取 URL?

目前,我有这段代码用于在我的 models.py 文件中获取 url:

    def get_absolute_url(self):
    return self.get_url()

这是我模板中的 cmets 部分代码:

    {% get_comment_count for page as comment_count %}

      <p>
      <a href="{% pageurl page %}#comments">
        {{ comment_count }} comment{{ comment_count|pluralize }}
      </a>
      {{ comment_count|pluralize:"has,have" }} been posted.
    </p>

    {% render_xtdcomment_tree for page %}
    {% render_comment_form for page %}

这是(希望)来自 django-cmets-xtd 的 comment_tree.html 的相关部分:

<h6 class="mb-1 small d-flex">
    <div class="mr-auto">{{ item.comment.submit_date }}&nbsp;-&nbsp;{% if item.comment.url and not item.comment.is_removed %}<a href="{{ item.comment.url }}" target="_new">{% endif %}{{ item.comment.name }}{% if item.comment.url %}</a>{% endif %}{% if item.comment.user and item.comment.user|has_permission:"django_comments.can_moderate" %}&nbsp;<span class="badge badge-secondary">{% trans "moderator" %}</span>{% endif %}&nbsp;&nbsp;<a class="permalink" title="{% trans 'comment permalink' %}" href="{% get_comment_permalink item.comment %}">¶</a></div>
    <span>
      {% if not item.comment.is_removed %}
        {% if perms.comments.can_moderate %}
          {% if item.flagged_count %}
            <span class="badge badge-danger" title="{% blocktrans count counter=item.flagged_count %}A user has flagged this comment as inappropriate.{% plural %}{{ counter }} users have flagged this comment as inappropriate.{% endblocktrans %}">{{ item.flagged_count }}</span>
          {% endif %}
        {% endif %}
        {% if allow_flagging and item.flagged %}
          <i class="fas fa-flag text-danger" title="{% trans 'comment flagged' %}"></i>
        {% elif allow_flagging %}
          <a class="mutedlink"
             href="{% url 'comments-flag' item.comment.pk %}">
            <i class="fas fa-flag" title="{% trans 'flag comment' %}"></i>
          </a>
        {% endif %}
        {% if perms.comments.can_moderate %}
          <a class="mutedlink"
             href="{% url 'comments-delete' item.comment.pk %}"><i class="fas fa-trash-alt" title="{% trans 'remove comment' %}"></i></a>
        {% endif %}
      {% endif %}
    </span>
  </h6>

【问题讨论】:

  • 作为后续,我在“站点”下添加了另一个站点,只是为了看看创建一个新站点是否能解决问题。当有两个站点“localhost”和“site.localhost”时,URL 表现良好并转到适当的站点。但是,如果我删除其中一个站点,它将恢复为使用“example.com”。所以我猜现在我需要调整或删除多站点设置。我不需要多站点。我认为它出现在默认的演示项目中......

标签: django comments wagtail django-comments


【解决方案1】:

Django 有自己的可选Sites framework,这与 Wagtail 的站点概念不同。在标准 Wagtail 项目模板中,这是关闭的(即 django.contrib.sites 被排除在 INSTALLED_APPS 之外)但您的项目可能已启用它,特别是如果您将 Wagtail 集成到现有 Django 项目中。像 Wagtail 一样,Django 的站点记录保存在数据库中,我怀疑这是 example.com 引用隐藏的地方 - 如果您启用了 Django 管理站点(与 Wagtail 不同),您应该找到列出的站点模型在那里。

挖掘django-cmets-xtd和django-contrib-comments代码可知{% get_comment_permalink %}标签返回的URL最终由django.contrib.contenttypes.views.shortcut视图处理,确实是listed as making use of the Django sites framework

至于为什么添加第二个 Wagtail 站点可以规避问题:当 Wagtail 生成页面 URL 时,它会更愿意返回没有域的本地 URL(例如 &lt;a href="/news/bloggy-test-post/"&gt;),因为它可以明确地这样做 - 就像只有定义了一个 Wagtail 站点。添加第二个站点后,它会切换到包含域的完整 URL - 此时,Django 认识到它已被传递完整 URL,并且不会尝试应用自己的站点逻辑来“修复”它。

【讨论】:

  • 感谢您的快速回复!我确实为我的用户身份验证安装了 django.contrib.sites,但我绝对没有意识到 Django 管理员和 Wagtail 管理员可以共存。但是既然你已经指出了它,那就有道理了。 Django 管理员确实已经在我的 urls.py 中设置并安装了应用程序,所以我刚刚登录并且出现了令人反感的“example.com”。链接现在一切正常。太感谢了。这让我发疯了。
猜你喜欢
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 2020-09-16
  • 2021-07-12
  • 2013-02-15
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多