TypeError:object()不带参数 - 尝试实现站点地图

时间:2016-02-28 11:02:54

标签: python django django-sitemaps

我正在浏览Hellowebapp教程,但当我尝试将网站地图实施到我的网络应用时,发生error。这是我的sitemap.py文件:

import datetime
from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
from .models import Thing

class ThingSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.5

    def items(self):
        return Thing.objects.all()

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

class StaticSitemap(Sitemap):
    lastmod = None
    priority = 0.5
    changefreq = "weekly"

    def items(self):
        return ['about', 'contact', 'browse',]

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

class HomepageSitemap(Sitemap):
    priority = 1
    changefreq = "daily"

    def items(self):
        return ['home',]

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

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

我已尝试过的东西:

  • 删除代码块(如不同的sitemap-classes)
  • 检查缩进(如果我在这里遗漏任何东西,我会感到羞耻)

非常感谢任何帮助!

根据要求,这是我的urls.py:

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth.views import password_reset, password_reset_done, password_reset_confirm, password_reset_complete
from django.views.generic import TemplateView, RedirectView
from django.contrib.sitemaps import Sitemap
from collection.sitemap import (
    ThingSitemap,
    StaticSitemap,
    HomepageSitemap,
)
sitemaps = {
    'things': ThingSitemap,
    'static': StaticSitemap,
    'homepage': HomepageSitemap,
}

from collection.backends import MyRegistrationView
from collection import views


urlpatterns = [
    url(r'^$', views.index, name='home'),
    url(r'^about/$',
        TemplateView.as_view(template_name='about.html'), name='about'),
    url(r'^contact/$', views.contact, name='contact'),


    url(r'^things/$', RedirectView.as_view(pattern_name='browse')),
    url(r'^things/(?P<slug>[-\w]+)/$', 
        views.thing_detail, name='thing_detail'),
    url(r'^things/(?P<slug>[-\w]+)/edit/$', 
        views.edit_thing, name='edit_thing'),

    url(r'^browse/$', RedirectView.as_view(pattern_name='browse')),
    url(r'^browse/name/$', 
        views.browse_by_name, name='browse'),
    url(r'^browse/name/(?P<initial>[-\w]+)/$', 
        views.browse_by_name, name='browse_by_name'),


    # password reset urls
    url(r'^accounts/password/reset/$', password_reset, 
        {'template_name': 'registration/password_reset_form.html'}, 
        name="password_reset"),
    url(r'^accounts/password/reset/done/$', 
        password_reset_done, 
        {'template_name': 'registration/password_reset_done.html'}, 
        name="password_reset_done"),
    url(r'^accounts/password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        password_reset_confirm, 
        {'template_name': 'registration/password_reset_confirm.html'}, 
        name="password_reset_confirm"),
    url(r'^accounts/password/done/$', password_reset_complete,
        {'template_name': 'registration/password_reset_complete.html'},
        name="password_reset_complete"),

    url(r'^accounts/register/$', 
        MyRegistrationView.as_view(), name='registration_register'),
    url(r'^accounts/create_thing/$', 
        views.create_thing, name='registration_create_thing'),

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


    url(r'^accounts/', include('registration.backends.simple.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

1 个答案:

答案 0 :(得分:0)

感谢bbayles'评论,我能够解决我的问题。通过文档浏览,我意识到我犯了很多错误,搞砸了我的进口。我没有从django.contrib.sitemaps.views导入,而是尝试从django.contrib.sitemaps导入。另外,我尝试导入Sitemap而不是sitemap