2

Django 3.0.8

Python 3.8.0

我该如何处理站点地图部分?

class EvergreenPostSitemap(Sitemap):
    changefreq = 'monthly'
    priority = 0.8
    protocol = 'https'
    def items(self):
        return Post.published.all().exclude(category_title="news")
    def lastmod(self, obj):
        return obj.updated


class NewsPostSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.9
    protocol = 'https'
    def items(self):
        return Post.published.filter(category_title="news")
    def lastmod(self, obj):
        return obj.updated


class AuthorSitemap(Sitemap):
    changefreq = 'yearly'
    priority = 0.5
    protocol = 'https'
    def items(self):
        return Author.published.all()
    def lastmod(self, obj):
        return obj.updated

sitemaps = {
    'evergreen': EvergreenPostSitemap,
    'news': NewsPostSitemap,
    'aurhors': AuthorSitemap,
}


urlpatterns = [
    path('sitemap.xml', views.index, {'sitemaps': sitemaps}),
    path('sitemap-news.xml', views.sitemap, {'sitemaps': sitemaps},
         name='django.contrib.sitemaps.views.sitemap'),
    path('sitemap-evergreen.xml', views.sitemap, {'sitemaps': sitemaps},
         name='django.contrib.sitemaps.views.sitemap'),
    path('sitemap-aurhors.xml', views.sitemap, {'sitemaps': sitemaps},
         name='django.contrib.sitemaps.views.sitemap'),
]


Environment:


Request Method: GET
Request URL: http://localhost:8000/sitemap.xml

Django Version: 3.0.8
Python Version: 3.8.0
Installed Applications:
['admin_aux',
 'images.apps.ImagesConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.sitemaps',
 'posts',
 'sidebars',
 'general',
 'categories',
 'marketing',
 'home',
 'authors',
 'taggit',
 'cachalot',
 'widgets',
 'code_samples',
 'hyper_links',
 'polls',
 'applications',
 'videos',
 'quotations',
 'languages',
 'people',
 'arbitrary_htmls.apps.ArbitraryHtmlsConfig',
 'tweets',
 'vk_posts',
 'facebook_posts',
 'instagram_posts',
 'email_subscriptions',
 'social_share',
 'assessments',
 'django_bootstrap_breadcrumbs']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/contrib/sitemaps/views.py", line 16, in inner
    response = func(request, *args, **kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/contrib/sitemaps/views.py", line 37, in index
    sitemap_url = reverse(sitemap_url_name, kwargs={'section': section})
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 677, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /sitemap.xml
Exception Value: Reverse for 'django.contrib.sitemaps.views.sitemap' with keyword arguments '{'section': 'evergreen'}' not found. 3 pattern(s) tried: ['sitemap\\-aurhors\\.xml$', 'sitemap\\-evergreen\\.xml$', 'sitemap\\-news\\.xml$']
4

1 回答 1

0

导致堆栈跟踪错误的行是

sitemap_url = reverse(sitemap_url_name, kwargs={'section': section})

这意味着它正在寻找一个名为“django.contrib.sitemaps.views.sitemap”和一个“section”的路径

所以它正在寻找类似的东西

path('sitemap/<str:section>', views.sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap')

注意<str:section>路径中的标记。这就是你告诉 Django 你的路径接受一个名为“section”的参数的方式。

于 2020-07-04T22:57:32.863 回答