简体   繁体   English

Django sitemap:“模块”对象没有属性“ get_urls”

[英]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.pysettings.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.

相关问题 在 django 中创建站点地图时出现“ArticleSitemap”object 没有属性“get_urls”错误 - getting 'ArticleSitemap' object has no attribute 'get_urls' error when creating sitemap in django Django 1.8 AttributeError:模块没有属性“ URL” - Django 1.8 AttributeError: module has no attribute 'urls' Django Sitemap — 'str' 对象没有属性 'get_absolute_url' 错误 - Django Sitemap — 'str' object has no attribute 'get_absolute_url' Error AttributeError:'module'对象没有属性'urls' - AttributeError: 'module' object has no attribute 'urls' 'Site'对象没有属性'flatpage_set'-Django Sitemap Framework - 'Site' object has no attribute 'flatpage_set' - Django sitemap framework 在Django错误上创建站点地图:对象没有属性'__getitem__' - Creating sitemap on Django error: object has no attribute '__getitem__' Django AttributeError: 'Alias' 对象没有属性 'urls' - Django AttributeError: 'Alias' object has no attribute 'urls' Django - AttributeError:'UserProfile'对象没有属性'urls' - Django - AttributeError: 'UserProfile' object has no attribute 'urls' Django-AttributeError:“模块”对象没有属性“ admin” - Django - AttributeError: 'module' object has no attribute 'admin' Django:“模块”对象没有属性“ ChoiceField”,“ MultipleChoiceField” - Django: 'module' object has no attribute 'ChoiceField', 'MultipleChoiceField'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM