Why my Sitemap show /number and not /title of my posts?

时间:2019-05-31 11:41:51

标签: python django sitemap

I´ve added the sitemap functionality to my blog, and it works fine. But in my /sitemap.xml file it shows not the title, but the number of the post. I tried to change the definition in my urls.py file to but then it shows some weird stuff after my domain. Maybe i have somewhere some little mistake

My main urls.py file:

from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import PostSitemap
from blog import views as blog_views

sitemaps = {
'posts': PostSitemap,
}

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
        path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
         name='django.contrib.sitemaps.views.sitemap'),
    path('<int:id>', blog_views.PostDetail, name='post'),
]

My sitemaps.py file:

from django.contrib.sitemaps import Sitemap
from .models import Post

class PostSitemap(Sitemap):

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

My models.py file:

from django.urls import reverse

def get_absolute_url(self):
    return reverse('post', args=[str(self.id)])

1 个答案:

答案 0 :(得分:0)

Oh, i found my mistake. I defined in my models.py file self.id. I changed that to self.slug and i changed my urls.py to <slug:slug>

Now it works fine.