Vinoth Kumar
Vinoth Kumar

Reputation: 31

Django SItemap Index NoReverseMatch at /sitemap.xml

I'm trying to implement Sitemap Index but I'm getting the below error on sitemap.xml. sitemap-posts.xml and sitemap-categories.xml works fine.

Error

NoReverseMatch at /sitemap.xml

Reverse for 'django.contrib.sitemaps.views.sitemap' with arguments '()' and keyword arguments '{'section': 'categories'}' not found. 0 pattern(s) tried: []

sitemap.py

from django.contrib.sitemaps import Sitemap

from .models import Post, Category

class PostSitemap(Sitemap):
    changefreq = 'daily'
    priority = 0.5

    def items(self):
        return Post.objects.published()

    def lastmod(self, obj):
        return obj.mod_date

class CategorySitemap(Sitemap):
    changefreq = 'daily'
    priority = 0.5

    def items(self):
        return Category.objects.all()

    def lastmod(self, obj):
        return obj.created_date

urls.py

from django.conf.urls import url
from django.contrib.sitemaps import views as sitemap

from . import views
from .sitemaps import PostSitemap, CategorySitemap

sitemaps = {
    'posts': PostSitemap,
    'categories': CategorySitemap,
}

urlpatterns = [
    url(r'^sitemap\.xml$', sitemap.index, { 'sitemaps': sitemaps }, 
        name='app1-sitemap'),
    url(r'^sitemap-(?P<section>.+)\.xml$', sitemap.sitemap, { 'sitemaps': sitemaps }, 
        name='django.contrib.sitemaps.views.sitemap1'),
]

Please help me to fix this error.

Upvotes: 2

Views: 2516

Answers (2)

dick lau
dick lau

Reputation: 1

I found the name of second URL conf. should be

name='django.contrib.sitemaps.views.sitemap' 

which refers to the official doc https://docs.djangoproject.com/en/1.11/ref/contrib/sitemaps/#creating-a-sitemap-index

instead of what you use

name='django.contrib.sitemaps.views.sitemap1'

and it works on my computer.

Upvotes: 0

keni
keni

Reputation: 1768

Change name from 'django.contrib.sitemaps.views.sitemap1' to 'sitemaps'. Apparently it uses that name for resolution.

name='django.contrib.sitemaps.views.sitemap1'

My urls look exactly like this and it works right, when I changed the name, I got an error close to yours.

from django.contrib.sitemaps.views import sitemap, index
    urlpatterns += [
            url(r'^sitemap\.xml$', cache_page(86400)(index), {'sitemaps': sitemaps, 'sitemap_url_name': 'sitemaps'}),
            url(r'^sitemap-(?P<section>.+)\.xml$', cache_page(86400)(sitemap), {'sitemaps': sitemaps}, name='sitemaps' )
        ]

sitemap_url_name required because I'm caching.

Upvotes: 2

Related Questions