Django站点地图:'模块'对象没有属性' get_urls'

时间:2017-03-24 16:41:15

标签: python django sitemap

得到这个错误,阻止我前进。遵循站点地图的标准设置并出现以下错误:

AttributeError at /sitemap.xml

'module' object has no attribute 'get_urls'

my urls.py:

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
import blog.views  as PostSiteMap
sitemaps ={
    'post' : PostSiteMap
}
urlpatterns = [

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

views.py:

class PostsSiteMap(Sitemap):
    changefreq = "daily"
    priority = 1.0
    def items(self):
        return Post.objects.all()

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

    def location(self, item):
        return reverse(item)

发布models.py:

class Post(models.Model):
title = models.CharField(max_length = 140)
body = RichTextUploadingField()
date = models.DateTimeField()
tags = models.ManyToManyField('Tags')
thumbnail = models.ImageField(upload_to="images/", null = False , default='images/place.png', blank = True, width_field="width_field",
        height_field="height_field")
height_field = models.IntegerField(default = 0, null = True, blank = True)
width_field = models.IntegerField(default = 0, null = True, blank = True)

def __str__(self):
    return self.title

def recent_posts(self):
    d = datetime.utcnow().replace(tzinfo=pytz.UTC) - timedelta(days=30)
    if self.date > d:
        return True
    else:
        return False

def get_absolute_url(self):
    return "/blog/%i/" % self.pk

有人有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

你得到的错误应该是你传递sitemaps字典,模块PostSiteMap本身而不是实际PostsSiteMap类(存在于{{{ 1}}模块)。

首先,您的站点地图应该位于名为PostSiteMap的单独文件中(这只是一个惯例和一个好的实践)。此文件应与sitemap.pywsgi.py等处于同一级别,因为它涉及整个项目的站点地图(这就是为什么它被称为站点地图!)。

settings.py(定义{{​​1}}课程)中,您应该这样:

views.py

现在,在PostsSiteMap中写下这些:

# blog/views.py

class PostsSiteMap(Sitemap):
    # your code as is

# This dictionary outside the class definition
SITEMAPS = {
    'post': PostsSiteMap,
}