سؤال

I am building a sitemap for Django. I mainly followed the instructions at djangoproject sitemaps

Here is my error message:

lib/python2.7/site-packages/django/core/paginator.py", line 45, in page
    return Page(self.object_list[bottom:top], number, self)
TypeError: 'Manager' object is not subscriptable

Here is my sitemap.py: from django.contrib.sitemaps import Sitemap from blog.models import Article

class BlogSitemap(Sitemap):
    changefreq = "monthly"
    priority = 0.5

    def items(self):
        return Article.objects

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

    def location(self, obj):
        return obj.get_absolute_url(False)

Here is my urls.py:

sitemaps = {
    'blog' : BlogSitemap,
    }
urlpatterns = ...
    ...),
      url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
        )

How do I go about getting my sitemap.xml up and running?

هل كانت مفيدة؟

المحلول

In BlogSitemap.items(), your return Article.objects which is a ModelManager. You want to return a queryset instead - ie Article.object.all() or Article.objects.filter(someconditionhere) etc

نصائح أخرى

I face the same problem in the Django.In the model, I define the ModelManager :

class WriterDraftManager(models.Manager):

    def get_queryset(self):
        return super().get_queryset().filter(pub="draft").order_by("-createdIn")

In the Viwset, I try to use the

class AritcleOpenView(APIView):

    @staticmethod
    def get(request):

        paginator = StandardSetPagination()
        query_set =Article.published
        page = paginator.paginate_queryset(query_set, request)
        serializer = ALLFormatSerializer(page, many=True, context=serializer_context)

        return paginator.get_paginated_response(serializer.data)

And it shows the error message like yours, so i change the

Article.published

to

Article.published.all()

and it works, hopes it can help you

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top