Dennis Gathagu
Dennis Gathagu

Reputation: 105

Django sitemaps 'Post' object has no attribute 'get_absolute_url'?

I'm creating a simple django 1.9 app that includes a blog and i want the site to have sitemaps consisting of the website pages and blog post entries.So far i am able to build a sitemap that generates the static pages as sitemaps but the blog post sitemap give me an error 'Post' object has no attribute 'get_absolute_url here is my code.Somebody please tell me where i'm going wrong.

sitemaps.py

from django.contrib import sitemaps

from django.contrib.sitemaps import Sitemap

from django.core.urlresolvers import reverse

from administration.models import Post


class StaticSitemap(sitemaps.Sitemap):

      changefreq = "daily"

      priority = 1.0



     def items(self):

          return ['administration:index', 'administration:about']



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



class PostSitemap(Sitemap):

      changefreq = "daily"

      priority = 0.5


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

models.py

class Post(models.Model):
    STATUS_CHOICES = (('draft', 'Draft'),('published', 'Published'),)
    featured_image = models.ImageField(upload_to="Photos/posts/images", blank=True, null=True)
    post_short_description = models.TextField(default="No description available")
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    category = models.ForeignKey(Category)
    tag = models.ManyToManyField(Tag)
    author = models.ForeignKey(User,related_name='blog_posts',on_delete=models.CASCADE)
    body = RichTextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES,default='draft')

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap
from administration.sitemaps import PostSitemap, StaticSitemap

sitemaps = {
   'post':PostSitemap,'static':StaticSitemap
}

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('administration.urls')),
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, 
    name='django.contrib.sitemaps.views.sitemap'),
    url(r'^robots\.txt', include('robots.urls')),
    url(r'^', include('cms.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 0

Views: 1689

Answers (1)

A. J. Parr
A. J. Parr

Reputation: 8026

When you use the Sitemap class and don't implement the location() method yourself, the default is to call get_absolute_url() on each object.

The docs at https://docs.djangoproject.com/en/2.1/ref/contrib/sitemaps/#a-simple-example say:

There is no location method in this example, but you can provide it in order to specify the URL for your object. By default, location() calls get_absolute_url() on each object and returns the result.

So you should either implement location() on your PostSitemap, or implement get_absolute_url() on your Post model.

Upvotes: 4

Related Questions