Friday, 26 December 2014

Generating Django sitemap.xml: How to fix 'get_absolute_url' error



I have a “BigPage” model that has an element called “pagename” that has unique set to True. I would like to use the django-sitemaps framework to generate, populate, and continuously update the sitemap.xml file every time a new BigPage model with a new “pagename” element is created by adding the URL myapp.com/pagename to my project’s sitemap.xml file. Here's my BigPage model:



class BigPage(models.Model):
Pagename = models.CharField(max_length=128, blank=True, unique=True, null=True) #they will enter this input into a form field to reserve their unique url at myapp.com/pagename
PageNameOwner = models.CharField(max_length=128, blank=True, null=True) #owner of page enters their name
OwnerGender = models.CharField(max_length=7, choices=(('male', 'Male'), ('female', 'Female')), blank=True, null=True)
PageViewsCounter = models.IntegerField(null=False, default=0)
PageIsRemoved = models.BooleanField(default=False) #true if mods take down a person’s page

def __unicode__(self):
return self.Pagename


I have created the below sitemap.py file and placed it in the folder of my app where the BigPage model resides:



class BigPageSitemap(Sitemap):
changefreq = 'daily'
priority = 0.5

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


Then, in the main project url.py file (not the app url.py file) I have added this:



sitemaps = {
'Name of Page':BigPageSitemap
}


To the urlpatterns element this:



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


My app url.py has the following url pattern where if a term is entered in the URL field that matches a pagename element that can then be queried to retrieve a BigPage object then it loads that page, but if the entered URL is not equal to a pagename element, it will give the user a 404:



url(r'^(?P<url_param>[a-zA-Z0-9_.-]*)/$', views.view_for_all_BigPages, name='view_for_all_BigPages'),)


After all of this, no sitemap file seems to generate if I check the app folder or main project folder. If I go to http://myapp.com/sitemap.xml I get the following error:



'BigPage' object has no attribute 'get_absolute_url'


What things have I done wrong? I really appreciate any help. I’ve been trying for days.


No comments:

Post a Comment