0

I'm trying to implement django sitemaps but i get the following error. I don't know what I'm doing wrong. Here is the relevant code and traceback.

File "mysite/sitemap.py" in location
  20.         return reverse(obj)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
  532.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  452.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /sitemap.xml
Exception Value: Reverse for 'name_of_url' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

here is sitemap.py file

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
from meddy1.models import Doctor
import datetime

class Sitemap(Sitemap):
    def __init__(self, names):
        self.names = names

    def items(self):
        return self.names

    def changefreq(self, obj):
        return 'weekly'

    def lastmod(self, obj):
        return datetime.datetime.now()

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


class DoctorSitemap(Sitemap):
    changefreq = "Daily"
    priority = 1

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

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

here is urls.py file

url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
James L.
  • 1,143
  • 22
  • 52
  • Read the documentation: https://docs.djangoproject.com/en/1.6/topics/http/urls/#reverse-resolution-of-urls . You forgot to give your url a name – karthikr Aug 09 '14 at 17:29
  • possible duplicate of [Django 1.6: name 'sitemaps' is not defined](http://stackoverflow.com/questions/25220561/django-1-6-name-sitemaps-is-not-defined) – petkostas Aug 09 '14 at 18:23

1 Answers1

0

If you read carefully the documentation: https://docs.djangoproject.com/en/1.6/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location

You will find out that Django calls get_absolute_url for the each Sitemap object (unless you have location specified). You define localtion with: reverse(obj) where does that point exactly? Your reverse should point to a valid registered url. Additionally where exactly did you read that location receives an argument? location is either an attribute or a method that returns a path (no arguments required).

Your error is not sitemaps related, but rather url resolution inside your registered sitemap models.

Finally what is the exact purpose of the Sitemap Class you define?

petkostas
  • 7,250
  • 3
  • 26
  • 29