Django Sitemap is not working gives error "Reverse for 'index' not found. 'index' is not a valid view function or pattern name."

100 views Asked by At

I'm trying to implement a Sitemap for my app and I get the error

"Reverse for 'index' not found. 'index' is not a valid view function or pattern name."

even though these views are set up.

What could cause this? I have no problem with dynamic sitemaps, just the static one.

My views.py


def front_page(request):
    return render(request, 'django_modules_testing_app/front_page.html', {})


def about_us(request):
    return render(request, 'ihgr_app/about_us.html', {})


def index(request):
    return render(request, 'django_modules_testing_app/index.html', {})

urls.py

from django.urls import path
from . import views
from . import blocks
from django.contrib.sitemaps.views import sitemap

from .sitemaps import DjangoModulesTestingApp

app_name = 'django_modules_testing_app'

sitemaps = {
    'main_app':DjangoModulesTestingApp
}

urlpatterns = [
    path('', views.front_page, name='front_page'),
    path('', views.index, name='index'),
    path('', views.about_us, name='about_us'),

    path('category_details/<slug:slug>/', blocks.category_details, name='category_details'),
    path('search_website/', views.SearchWebsite.as_view(), name='search_website'),

    path('sitemap.xml', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap')
]

sitemaps.py

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


class StaticSitemap(Sitemap): 
    changefreq = 'weekly'
    priority = 0.8
    protocol = 'http'

    def items(self):
        return ['front_page','index','about_us']  

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

There are 1 answers

0
Ankit Tiwari On

You've to specify your app name inside reverse(...) like this

class StaticSitemap(Sitemap): 
    changefreq = 'weekly'
    priority = 0.8
    protocol = 'http'

    def items(self):
        return ['front_page','index','about_us']  

    def location(self, item):
        return reverse(f"django_modules_testing_app:{item}")