djangoxml-sitemap

Django NoReverseMatch at /sitemap.xml error for static views


I followed the instruction on django documentation, but I am getting this error when trying to create the sitemap.xml for my app. (also the similar questions on stackoverflow were not describing my case)

enter image description here

The html pages that I would like to add to the sitemap are not based on any models, but they have some forms in their footers. (That's why the urls don't contain "as_view()". I have also added

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    'django.contrib.sitemaps',
    '''
]

to settings.py file.

Here are more details:

App --> main

main/urls.py

from django.urls import path
from . import views

app_name = 'main'

urlpatterns = [
    path('', views.HomeView, name='homepage'),
    path('about/', views.AboutView, name='about'),
    path('contact/', views.Contact, name='contact'),
]

main/sitemap.py

from django.contrib.sitemaps import Sitemap
from django.urls import reverse

class StaticViewSitemap(Sitemap):
    changefreq = 'daily'
    priority = 0.9

    def items(self):
        return ['homepage','about','contact']

    def location(self, item):
        return reverse(item)

Project --> dcohort

dcohort/urls.py

from argparse import Namespace
from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps.views import sitemap
from main.sitemaps import StaticViewSitemap
from django.conf import settings
from django.conf.urls.static import static

sitemaps = {
    'staticviews': StaticViewSitemap,
}

urlpatterns = [
    path('', include('main.urls', namespace='main')),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),
]

Solution

  • I finally figured the problem. I should have appended the app name to the page url name.

    The main/sitemap.py would be:

    from django.contrib.sitemaps import Sitemap
    
    from django.urls import reverse
    
    class StaticViewSitemap(Sitemap):
        changefreq = 'daily'
        priority = 0.9
    
        def items(self):
            return ['main:homepage','main:about','main:contact']
    
        def location(self, item):
            return reverse(item)