Craig O'Toole
Craig O'Toole

Reputation: 3

Django sitemap : 'module' object has no attribute 'get_urls'

getting this error that's stopping me from progressing. Followed standard setup for a sitemap and got the following error:

AttributeError at /sitemap.xml

'module' object has no attribute 'get_urls'

my urls.py:

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
import blog.views  as PostSiteMap
sitemaps ={
    'post' : PostSiteMap
}
urlpatterns = [

    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
    name='django.contrib.sitemaps.views.sitemap')
]

views.py:

class PostsSiteMap(Sitemap):
    changefreq = "daily"
    priority = 1.0
    def items(self):
        return Post.objects.all()

    def lastmod(self, obj): 
       return obj.date

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

Post models.py:

class Post(models.Model):
title = models.CharField(max_length = 140)
body = RichTextUploadingField()
date = models.DateTimeField()
tags = models.ManyToManyField('Tags')
thumbnail = models.ImageField(upload_to="images/", null = False , default='images/place.png', blank = True, width_field="width_field",
        height_field="height_field")
height_field = models.IntegerField(default = 0, null = True, blank = True)
width_field = models.IntegerField(default = 0, null = True, blank = True)

def __str__(self):
    return self.title

def recent_posts(self):
    d = datetime.utcnow().replace(tzinfo=pytz.UTC) - timedelta(days=30)
    if self.date > d:
        return True
    else:
        return False

def get_absolute_url(self):
    return "/blog/%i/" % self.pk

anybody have any ideas why? thanks!

Upvotes: 0

Views: 1166

Answers (1)

nik_m
nik_m

Reputation: 12106

The error you get is ought to the fact that you are passing inside the sitemaps dictionary, the module PostSiteMap itself instead of the actual PostsSiteMap class (that lives inside the PostSiteMap module).

First of all, your sitemaps should live in a separate file called sitemap.py (this is just a convention and a good pracice). This file should live on the same level as wsgi.py, settings.py etc, because it concerns the sitemap of the whole project (that's why it's called sitemap!).

In your views.py (which are defining the PostsSiteMap class) you should right something like this:

# blog/views.py

class PostsSiteMap(Sitemap):
    # your code as is

# This dictionary outside the class definition
SITEMAPS = {
    'post': PostsSiteMap,
}

Now, in your urls.py write these:

# urls.py

from django.conf.urls import url, include
....
from blog.views import SITEMAPS

urlpatterns = [
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': SITEMAPS}, name='django.contrib.sitemaps.views.sitemap')
]

Upvotes: 1

Related Questions