[英]Django sitemap : 'module' object has no attribute 'get_urls'
getting this error that's stopping me from progressing. 收到此错误,这使我无法继续前进。 Followed standard setup for a sitemap and got the following error: 遵循了站点地图的标准设置,并出现以下错误:
AttributeError at /sitemap.xml
'module' object has no attribute 'get_urls'
my urls.py: 我的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: 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)
Post models.py: 发表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
anybody have any ideas why? 有人有什么想法吗? thanks! 谢谢!
The error you get is ought to the fact that you are passing inside the sitemaps
dictionary, the module PostSiteMap
itself instead of the actual PostsSiteMap
class (that lives inside the PostSiteMap
module). 您收到的错误应该是由于您正在传递sitemaps
字典中的信息,即模块PostSiteMap
本身而不是实际的PostsSiteMap
类(位于PostSiteMap
模块中)。
First of all, your sitemaps should live in a separate file called sitemap.py
(this is just a convention and a good pracice). 首先,您的站点地图应该位于一个名为sitemap.py
的单独文件中(这只是一个约定,也是一个很好的实践)。 This file should live on the same level as wsgi.py
, settings.py
etc, because it concerns the sitemap of the whole project (that's why it's called sitemap!). 该文件应与wsgi.py
, settings.py
等处于同一级别,因为它涉及整个项目的站点地图(这就是为什么将其称为站点地图!)。
In your views.py
(which are defining the PostsSiteMap
class) you should right something like this: 在您的views.py
(定义PostsSiteMap
类)中,您应该执行以下操作:
# blog/views.py
class PostsSiteMap(Sitemap):
# your code as is
# This dictionary outside the class definition
SITEMAPS = {
'post': PostsSiteMap,
}
Now, in your urls.py
write these: 现在,在您的urls.py
编写以下代码:
# urls.py
from django.conf.urls import url, include
....
from blog.views import SITEMAPS
urlpatterns = [
url(r'^sitemap\.xml$', sitemap, {'sitemaps': SITEMAPS}, name='django.contrib.sitemaps.views.sitemap')
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.