我的网站地图页面存在奇怪的问题。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import urllib
from django.core.urlresolvers import reverse
from django.contrib.sitemaps import Sitemap
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from pytz import UTC
from mychat.models import Question
MONTHLY = 'monthly'
WEEKLY = 'weekly'
DAILY = 'daily'
HOURLY = 'hourly'
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
# The following lists are separated only for fixed ordering.
class RecentForumViewPaginationSitemap(Sitemap):
"""
Sitemap generator that takes Django Recent tab in forum list view and
generates all pages in pagination.
"""
changefreq = DAILY
priority = 0.90
protocol = 'https'
def items(self):
post_queryset = Question.objects.all().order_by('-created')
paginator = Paginator(post_queryset, 12)
return paginator.page_range
def location(self, page):
return '?'.join([reverse('question_list'), urllib.urlencode({'recent_page': page})])
sitemaps = {
'recent_forum_views': RecentForumViewPaginationSitemap,
}
最近升级到Django 1.9.7之后它才会被推荐
urls.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf.urls import patterns, url
from my.sitemap import sitemaps
urlpatterns = patterns(
'',
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps,
'template_name': 'my/custom_sitemap.xml'}),
)
custom_sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
{% spaceless %}
{% for url in urlset %}
<url>
<loc>{{ url.location }}</loc>
{% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
</url>
{% endfor %}
{% endspaceless %}
</urlset>
我正在追溯:
ERROR base.handle_uncaught_exception.284 Internal Server Error: /sitemap.xml
Traceback (most recent call last):
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/contrib/sitemaps/views.py", line 16, in inner
response = func(request, *args, **kwargs)
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/contrib/sitemaps/views.py", line 67, in sitemap
protocol=req_protocol))
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/contrib/sitemaps/__init__.py", line 104, in get_urls
urls = self._urls(page, protocol, domain)
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/contrib/sitemaps/__init__.py", line 112, in _urls
for item in self.paginator.page(page).object_list:
File "/home/bartosz/.virtualenvs/my9/local/lib/python2.7/site-packages/django/core/paginator.py", line 55, in page
return self._get_page(self.object_list[bottom:top], number, self)
TypeError: sequence index must be integer, not 'slice'
我想知道最近是否有人遇到类似的问题,或者能指出我正确的方向来解决它。谢谢你的任何建议。
答案 0 :(得分:1)
我认为这是你向后兼容的变化:
要解决此问题,请从items()
方法返回列表:
def items(self):
post_queryset = Question.objects.all().order_by('-created')
paginator = Paginator(post_queryset, 12)
return list(paginator.page_range)
注意:我远不是Django和Sitemap框架的专家 - 可能有更好的方法来解决这个问题。我希望这至少会成为迈向正确方向的一步。