Implementing sitemaps in Django

I am having a problem implementing sitemaps in my application. I am using Virtualenv, django 1.4 and Python 2.7. I would appreciate it if you could help me solve this problem.

This is what I did:

  • In my urls.py

    from sitemap import JobPostSitemap sitemaps = { 'jobs': JobPostSitemap, } ... # Removed other urls url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), 
  • Then in sitemap.py

     from django.contrib.sitemaps import Sitemap from jobs.models import JobPost class JobPostSitemap(Sitemap): changefreq = "never" priority = 0.5 def items(self): return JobPost.objects.filter(approved=True) def lastmod(self, obj): return obj.pub_date 
  • My settings.py file looks like this:

     TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) ... INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sitemaps', 'jobs', ) ... 

Now when I open the browser and go to http://localhost:8000/sitemap.xml , I get the following error:

 ImportError at /sitemap.xml No module named django.contrib.sitemaps Request Method: GET Request URL: http://localhost:8000/sitemap.xml Django Version: 1.4.2 Exception Type: ImportError Exception Value: No module named django.contrib.sitemaps Exception Location: /home/frank/Projects/python/django/techjobsea.com/baseline27/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/utils/importlib.py in import_module, line 35 Python Executable: /home/frank/Projects/python/django/techjobsea.com/baseline27/bin/python Python Version: 2.7.3 

I can’t understand what I missed or did wrong.

+8
python django sitemap
source share
4 answers

I had a similar error. I changed the urls.py definition as follows:

 from sitemap import JobPostSitemap from django.contrib.sitemaps.views import sitemap sitemaps = { 'jobs': JobPostSitemap, } ... # Removed other urls url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}), 

And it worked for me. I do not know why...

+17
source share

This may be a PYTHONPATH problem. Run python manage.py shell and try import django.contrib.sitemaps

0
source share

The problem is probably the url.py configuration that you did not send completely. In my case, I accidentally left the form prefix: urlpatterns = patterns('...') , which prevented Django from finding the correct path.

0
source share

To activate Sitemap creation on your Django site, add this line to your URLconf:

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

This tells Django to create a sitemap when the client accesses / sitemap.xml.

-2
source share

All Articles