【问题标题】:Tags not found in Wagtail Blog: Using the URLconf defined in mysite.urls, Django tried these URL patterns在 Wagtail 博客中找不到标签:使用 mysite.urls 中定义的 URLconf,Django 尝试了这些 URL 模式
【发布时间】:2019-01-29 06:11:43
【问题描述】:

我正在关注Wagtail v2.4 tutorial 并向博客添加了标记功能。但是,当我单击博客文章上的标签链接时,我收到 404 错误。标签索引页面 (/tags/) 显示所有没有任何标签的帖子。我只创建了一个带有标签的帖子。

找不到页面 (404)

请求方法:GET

请求网址:http://localhost:8000/tags/tag%3Dnewswithimages

使用 koamrn.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

  1. ^django-admin/
  2. ^admin/
  3. ^文档/
  4. ^search/$ [name='search']
  5. ^_util/authenticate_with_password/(\d+)/(\d+)/$[name='wagtailcore_authenticate_with_password']
  6. ^_util/login/$ [name='wagtailcore_login']
  7. ^((?:[\w-]+/)*)$ [name='wagtail_serve']
  8. ^static/(?P.*)$
  9. ^media/(?P.*)$

当前路径 tags/tag=newswithimages 与其中任何一个都不匹配。

我猜我需要在urls.py 中添加路径?

from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin

from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls

from search import views as search_views

urlpatterns = [
    url(r'^django-admin/', admin.site.urls),

    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),

    url(r'^search/$', search_views.search, name='search'),

    # For anything not caught by a more specific rule above, hand over to
    # Wagtail's page serving mechanism. This should be the last pattern in
    # the list:
    url(r'', include(wagtail_urls)),

    # Alternatively, if you want Wagtail pages to be served from a subpath
    # of your site, rather than the site root:
    #    url(r'^pages/', include(wagtail_urls)),
]


if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# Serve static and media files from development server
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

news_tag_index_page.html:

{% extends "base.html" %}
{% load wagtailcore_tags %}

{% block content %}
    {% if request.GET.tag|length %}
        <h4>Showing pages tagged "{{ request.GET.tag }}"</h4>
    {% endif %}

    {% for newspage in newspages %}

        <p>
            <strong><a href="{% pageurl newspage %}">{{ newspage.title }}</a></strong><br />
            <small>Revised: {{ newspage.latest_revision_created_at }}</small><br />
            {% if newspage.author %}
                <p>By {{ newspage.author.profile }}</p>
            {% endif %}
        </p>

    {% empty %}
        No pages found with that tag.
    {% endfor %}
{% endblock %}

【问题讨论】:

    标签: wagtail


    【解决方案1】:

    标签列表页面的 URL 应该是 /tags?tag=newswithimages,而不是 tags/tag=newswithimages。很可能,您在blog_page.html 的链接中打错了:

    <a href="{% slugurl 'tags' %}?tag={{ tag }}"><button type="button">{{ tag }}</button></a>
    

    【讨论】:

    • 是的!我错过了tag={{ tag }} 之前的问号。谢谢@gasman!
    【解决方案2】:

    你只需要继续阅读教程:)

    “访问带有标签的博客文章现在应该在底部显示一组链接按钮 - 每个标签都有一个按钮。但是,单击按钮会得到 404,因为我们尚未定义“标签”视图。”

    https://docs.wagtail.io/en/v2.4/getting_started/tutorial.html#tagging-posts

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 2018-06-28
      • 2017-05-27
      • 2020-09-01
      • 2020-08-02
      • 2021-02-20
      • 2021-05-11
      • 2017-06-26
      • 2020-05-05
      相关资源
      最近更新 更多