ImportError:没有名为sitemaps的模块

时间:2014-03-06 16:20:49

标签: python django sitemap

我从Django lib导入站点地图有问题

我的sitemap.py

from django.contrib.sitemaps import Sitemap
from django.db import models

YN_CHOICES = (
    (u'Y', u'Yes'),
    (u'N', u'No'),
)

class Content(models.Model):
    title = models.CharField(max_length=150)
    published = models.CharField(max_length=1, default='N', choices=YN_CHOICES)
    content = models.TextField()
    created = models.DateTimeField()

    class Meta:
        verbose_name = 'Content'
        verbose_name_plural = 'Contents'

    def __unicode__(self):
        return self.title

class SitemapXML(Sitemap):
    changefreq = 'weekly'
    priority = 0.5

    def items(self):
        return Content.objects.filter(published='Y').order_by('id')

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

    def location(self, obj):
        return "/content/%s/" % obj.id

我的settings.py

    TEMPLATE_LOADERS = (
      'django.template.loaders.filesystem.Loader',
      'django.template.loaders.app_directories.Loader',
      # 'django.template.loaders.eggs.Loader',
    )
INSTALLED_APPS = (
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.sites',
  'django.contrib.sitemaps ',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'django.contrib.admin',
  'django.contrib.webdesign',
  'django.contrib.comments',
  'django.contrib.humanize',
)

main urls.py

from pinim.sitemap import SitemapXML    
from django.contrib.sitemaps.views import sitemap
    sitemaps = {'main':SitemapXML}

urlpatterns = patterns('',
  name='server_status'),
  url('imprint', 'blog.views.imprint', name='homepage_imprint'),
  url('archive', 'blog.views.archive', name='homepage_archive'),
  url(r'^masterpage.html$', TemplateView.as_view(template_name='masterpage.html'), name='masterpage'),
  #(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps':sitemaps}),
  url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
  url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
  url(r'^admin/', include(admin.site.urls)),      
)
....

但是当我在终端中运行服务器时,我遇到错误“ImportError:没有名为sitemaps的模块”。

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/south/management/commands/__init__.py", line 10, in <module>
    import django.template.loaders.app_directories
  File "/home/av/vp/pin-im/local/lib/python2.7/site-packages/django/template/loaders/app_directories.py", line 25, in <module>
    raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError django.contrib.sitemaps : No module named sitemaps 

站点地图存在于Django的包contrib中并连接到项目

1 个答案:

答案 0 :(得分:2)

我认为问题出在你的settings.py文件中。 &#39; django.contrib.sitemaps&#39;有一个额外的空间。在INSTALLED_APPS中。它应该是&#39; django.contrib.sitemaps&#39;。