3

I'm trying to implement sitemaps in my django application but i get the following error. I'm using the django sitemap's framework. I don't know what I'm doing wrong.

Traceback:
File "mysite/urls.py" in <module>
  3. from sitemap import *
File "mysite/sitemap.py" in <module>
  5. class Sitemap(sitemaps.Sitemap):

Exception Type: NameError at /
Exception Value: name 'sitemaps' is not defined

Here is the sitemap.py file

from django.contrib.sitemaps import Sitemap
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 the urls.py file

from django.conf.urls import patterns, include, url
from django.contrib import admin
from sitemap import *

admin.autodiscover()

sitemaps = {
    'pages':Sitemap(['homepage_imprint', 'homepage_archive']),
    'doctor':DoctorSitemap,
    'site':Sitemap(['name_of_url', 'name_of_url']),
}

urlpatterns = patterns('',
    url(r'^', include('meddy1.urls')),
    url(r'^', include('django.contrib.auth.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^sitemap\.xml', include('django.contrib.sitemaps.views.sitemap'),{'sitemaps': sitemaps}), 
)
James L.
  • 1,143
  • 22
  • 52

3 Answers3

2

You imported Sitemaps from the module, not the module itself. Remove the module name:

class Sitemap(Sitemap):

This will just about work, even though you are replacing the imported class here.

Alternatively and arguably clearer as to what you are doing, adjust your import of the module. Change the import from:

from django.contrib.sitemaps import Sitemap

to:

from django.contrib import sitemaps
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • When I remove the module name and make it to Class Sitemap(Sitemap): I get importError: No module named Sitemap. Then I tried and changed the import as you suggested to from django.contrib import sitemaps and now it give me name 'Sitemap' is not defined. – James L. Aug 09 '14 at 16:27
  • @HarisB.: For the changed import you wouldn't change the class line; leave that at `class Sitemap(sitemaps.Sitemap):`. – Martijn Pieters Aug 09 '14 at 16:28
  • @HarisB.: the import error sounds as if you changed the `import` line incorrectly and removed `sitemaps` from that too. Either change the import **or** change the class definition, not both. – Martijn Pieters Aug 09 '14 at 16:28
  • I only changed the class definition (see code above), but I'm still getting no module name sitemap error. – James L. Aug 09 '14 at 16:37
  • Not sure how you managed to get an import error out of this then. `sitemap` is singular? Can you show the full traceback? – Martijn Pieters Aug 09 '14 at 17:01
0

Martijn already provided correct answer, I just want to add a more general note about namespaces in Python: every name you use in Python has to come from somewhere. There is a number of built-in names that are always available, e.g. dir(). Other than the built-ins, every name has to be either created in your own code in the module OR imported from some other module or package:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = 1
>>> x
1
>>> sys
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> import sys
>>> sys
<module 'sys' (built-in)>
Rainy
  • 1,066
  • 8
  • 14
0

Simply change the s in sitemap to lowercase and add .views to the import line. therefore the new import should be "from django.contrib.sitemaps.views import sitemap"