在Django中实现站点地图

Fra*_*ine 8 python sitemap django

我在应用程序中实现站点地图时遇到问题.我正在使用Virtualenv,django 1.4和Python 2.7.如果你能帮我解决这个问题,我将不胜感激.

这就是我所做的:

  1. 在我的urls.py中

    from sitemap import JobPostSitemap
    sitemaps = { 
        'jobs': JobPostSitemap, 
    }
    ... # Removed other urls
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后在我的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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我的settings.py文件如下:

    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', 
    )
    ...
    
    Run Code Online (Sandbox Code Playgroud)

现在当我打开浏览器并导航到时http://localhost:8000/sitemap.xml,我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚我错过了什么或做错了什么.

Mur*_*rlu 18

我有类似的错误.我更改了urls.py定义:

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

它对我有用.我不知道为什么......