Implementing sitemaps in Django
Asked Answered
C

4

8

I'm having a problem implementing the sitemaps in my application. I'm using Virtualenv, django 1.4 and Python 2.7. I would appreciate if you can help me resolve this.

This is what I have done:

  1. 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}),
    
  2. Then in my sitemap.py file

    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
    
  3. My settings.py file is as follows:

    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 my browser and navigate 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 figure out what I've missed or doing wrong.

Cairns answered 17/12, 2012 at 9:39 Comment(3)
Have you followed all the required installation steps for sitemap? The required steps hereAudun
@Audun Yes. I followed the all the steps.Cairns
@MuratÇorlu No, I didn't. Can't explain why its not working.Cairns
H
18

I had similar error. I changed urls.py definition like that:

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 don't know why...

Higginbotham answered 24/5, 2013 at 7:35 Comment(2)
Dude, this actually worked. Thanks. I will be interested to know why this works as opposed to the other method. Sweeet!Cairns
Did the patterns call have a non-empty prefix parameter? Something like urlpatterns = patterns('some.prefix', url(r'...'), ....)? This could have been your problem.Maine
R
0

It may be a PYTHONPATH issue. Run python manage.py shell and try to import django.contrib.sitemaps

Retina answered 17/12, 2012 at 10:23 Comment(0)
C
0

The problem probably lies in the url.py config that you have not wholly submitted. In my case I had inadvertently left a prefix of the form: urlpatterns = patterns('...') that prevented Django from finding the right path.

Came answered 9/1, 2013 at 11:28 Comment(1)
Thanks, but I have included that section. All the urls work fine except the sitemap.Cairns
I
-2

To activate sitemap generation on your Django site, add this line to your URLconf:

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

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

Insane answered 17/12, 2012 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.