1

I used to have a working sitemap before I upgraded to Wagtail 2.4. I have upgraded my site according to the documentation here: http://docs.wagtail.io/en/v2.4/reference/contrib/sitemaps.html but still no such luck. The error I'm getting is get_sitemap_urls() takes 1 positional argument but 2 were given.

I've added 'django.contrib.sitemaps', to my INSTALLED_APPS in my base settings, and I've added the following to my urls.py:

from wagtail.contrib.sitemaps.views import sitemap

urlpatterns = [
    ...

    url('^sitemap\.xml$', sitemap),

    ...
]

My Page models have the following included in them:

def get_sitemap_urls(self, request=None):
        return [
            {
                'location': self.get_full_url(request),
                'lastmod': (self.last_published_at or self.latest_revision_created_at),
                'changefreq': 'monthly',
                'priority': .5,
            }
        ]

I previously had a sitemap.xml file included to overwrite the old wagtailsitemaps/sitemap.xml that looked like this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% 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>

Traceback:

django_1    | Traceback (most recent call last):
django_1    |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
django_1    |     response = get_response(request)
django_1    |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
django_1    |     response = self.process_exception_by_middleware(e, request)
django_1    |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
django_1    |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1    |   File "/usr/local/lib/python3.6/site-packages/wagtail/contrib/sitemaps/views.py", line 17, in sitemap
django_1    |     return sitemap_views.sitemap(request, sitemaps, **kwargs)
django_1    |   File "/usr/local/lib/python3.6/site-packages/django/contrib/sitemaps/views.py", line 16, in inner
django_1    |     response = func(request, *args, **kwargs)
django_1    |   File "/usr/local/lib/python3.6/site-packages/django/contrib/sitemaps/views.py", line 71, in sitemap
django_1    |     protocol=req_protocol))
django_1    |   File "/usr/local/lib/python3.6/site-packages/django/contrib/sitemaps/__init__.py", line 111, in get_urls
django_1    |     urls = self._urls(page, protocol, domain)
django_1    |   File "/usr/local/lib/python3.6/site-packages/wagtail/contrib/sitemaps/sitemap_generator.py", line 42, in _urls
django_1    |     url_info_items = item.get_sitemap_urls(self.request)
django_1    | TypeError: get_sitemap_urls() takes 1 positional argument but 2 were given
django_1    | [27/Mar/2019 20:26:13] "GET /sitemap.xml HTTP/1.1" 500 91336

Any idea what I'm missing? I have done everything the documentation said to do, and it's pretty basic. I can't find anything else online that includes anything other than these steps.

kbdev
  • 1,225
  • 1
  • 13
  • 33
  • Can you give the full stack trace of the error, please? Also, what version of Django are you running (and did you upgrade it as part of the Wagtail upgrade - if so, what version were you on before)? – gasman Mar 27 '19 at 21:49
  • @gasman I added the traceback in my original post. Django was upgraded to 2.1 when I upgraded to Wagtail 2.4. The versions I had before were `Wagtail 2.0` and `Django 1.11.10`. – kbdev Mar 28 '19 at 13:26
  • 5
    Are you sure that the `get_sitemap_urls` method on all your page models is defined as `def get_sitemap_urls(self, request=None):`? The error would seem to suggest that at least one of them isn't set up to accept a `request` argument. – gasman Mar 28 '19 at 17:14
  • @gasman D'oh! Seems that I had missed one and that was the issue. I wasn't sure if I was doing something wrong or if `request=None` needed to be explicitly set or not. I guess it does need to be set on all methods or this error will occur. Thank you! – kbdev Mar 28 '19 at 17:48

1 Answers1

0

Try this:

def get_sitemap_urls(self, obj):
    return obj.last_published_at