tim tim - 5 years ago 489
Python Question

get_absolute_url - Django`s sitemap

I want to enable sitemap-generation in Django, so I do the following, how it explained here

model:

class Car(models.Model):
def __unicode__(self):
return self.name
name = models.CharField('Name', max_length=10)
active = models.BooleanField()


urls:

car_dict = {
'queryset': Car.objects.filter(active=1),
}

sitemaps = {
#'flatpages': FlatPageSitemap,
'car': GenericSitemap(car_dict, priority=0.5),
}


But I have an error on /sitemap.xml: 'Car' object has no attribute 'get_absolute_url. How to fix it? I need to create some classes, how it explained here? Or I can use only GenericSitemap? I commented 'flatpages', because I dont use them. Thanks.

Update 1:
in URLS.py:

url(r'^car/$', 'cars.views.shop'),
url(r'^car/(?P<car_id>\d+)/$', 'cars.views.producer'),

Answer Source

You have to define get_absolute_url for the model:

from django.core.urlresolvers import reverse

class Car(models.Model):
    name = models.CharField('Name', max_length=10)
    active = models.BooleanField()

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('car_details', kwargs={'name':self.name})

Please update the URL reverse method according to the URL you have defined.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download