How to add Trailing slash (/) in url of sitemap Django?

160 views Asked by At

I want to add (/) in site map of Django. I have used following code to generate sitemap in django

my url.py is

from django.contrib.sitemaps.views import sitemap
from myApp.sitemaps import staticSitemap , mySitemap

sitemaps = {
'staticSitemap':staticSitemap,
'mySitemap':mySitemap
}

urlpatterns = [
    path('admin/', admin.site.urls),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps} ),
    path('<slug:slug>/',apps.switcher, name='calc_detail'),
]

my sitemap file is like below

from django.contrib.sitemaps import Sitemap
from django.shortcuts import reverse
from .models import SingleCalculator

class staticSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.9
    def items(self):
        return ['home','about','contact','privacy']
    def location(self, item):
        return reverse(item)


class mySitemap(Sitemap):
        changefreq = "weekly"
        priority = 0.7
        
        def items(self):
            return SingleCalculator.objects.all()
        def lastmod(self,obj):
            return obj.lastmod

Sitemap is generating now like following URL in loc

<loc>https://sitename.com/absolute-difference-calculator</loc>

I want (/) after the end of url. How can I do this?

1

There are 1 answers

0
Kd Johar On

In model class SingleCalculator add get_absolute_url function:

def get_absolute_url(self):
        return f'/{self.slug}/'