Tekano Khambane
Tekano Khambane

Reputation: 15

How to change from request.site to Site._find_for_request(request) in wagtail 2.9

I've recently upgraded my wagtail app to the latest version, version 2.9, now since the wagtail site middleware has been deprecated I'm having trouble getting my site to work. I was using a request.site now wagtail 2.9 uses Site._find_for_request(request)

How do I change my current code to work with the latest wagtail version? Thanks in advance

@register.simple_tag(takes_context=True)

def og_image(context, page):

protocol = re.compile(r'^(\w[\w\.\-\+]*:)*//')

if protocol.match(settings.MEDIA_URL):
    base_url = ''
else:
    base_url = context['request'].site.root_url

if page:
    if page.og_image:
        return base_url + page.og_image.get_rendition('original').url
    elif page.cover_image:
        return base_url + page.cover_image.get_rendition('original').url
if LayoutSettings.for_site(context['request'].site).logo:
    layout_settings = LayoutSettings.for_site(context['request'].site)
    return base_url + layout_settings.logo.get_rendition('original').url
return None

Upvotes: 2

Views: 1452

Answers (1)

acarasimon96
acarasimon96

Reputation: 303

It looks like that you're still doing the old way of getting current site, in this case:

context['request'].site

where context['request'] is the request object. Change those occurrences to below as recommended in the Wagtail 2.9 release notes:

Site.find_for_request(context['request'])

Don't forget to make sure that you have from wagtail.core.models import Site somewhere on top of the file or else your code won't work, i.e. you get an error.

You can find more info on Site.find_for_request in the docs.

Upvotes: 8

Related Questions