0

我正在阅读 Hellowebapp 教程,但是当我尝试将站点地图实现到我的 Web 应用程序时发生错误。这是我的 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)

我已经尝试过的东西:

  • 删除代码块(如不同的站点地图类)
  • 检查缩进(如果我在这里遗漏了任何东西,我会感到羞耻)

任何帮助将非常感激!

根据请求,这里是我的 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)),
]
4

1 回答 1

0

感谢bbayles的评论,我能够解决我的问题。浏览文档,我意识到我犯了一个菜鸟错误,就是搞砸了我的导入。我没有从导入,而是django.contrib.sitemaps.views尝试从django.contrib.sitemaps. 另外,我尝试导入Sitemap而不是sitemap.

于 2016-02-28T16:32:58.633 回答