wagtail-headless-preview icon indicating copy to clipboard operation
wagtail-headless-preview copied to clipboard

AttributeError when page not in site

Open jams2 opened this issue 1 year ago • 2 comments

AttributeError: 'NoneType' object has no attribute 'root_url'

https://github.com/torchbox/wagtail-headless-preview/blob/d771f4c26d61652af1aadcbdb84ef201b1da3850/src/wagtail_headless_preview/models.py#L48-L54

If a page hasn't been added to a site yet (i.e. it is not the descendant of any page that is the root page of a Site, perhaps as it was added as a child of the root page), attempting to preview it results in an AttributeError. This is likely due to Page.get_site returning None, which is passed through to get_client_root_url_from_site, which attempts to access the root_url attribute.

This can occur as to create a Site a user must select a root page, for which purpose they may create a new page.

jams2 avatar May 17 '24 14:05 jams2

Related to this but possibly a different issue – I see in #59 we switch from using Page.get_site() to Site.find_for_request(request). Which I assume would fix this issue, but doesn’t seem correct to me either 🤔

thibaudcolas avatar Jun 18 '24 15:06 thibaudcolas

The overreliance on Site is one of those little bugbears that Matthew talked about at Wagtail Space NL.

Happy to review a PR that makes this more robust

zerolab avatar Jun 18 '24 16:06 zerolab

@zerolab what do you think about providing a configuration variable that either:

  • lets developers specify a fallback client URL; or
  • lets developers opt-in to falling back to the client URL configured for the default site?

I'm wary of the proliferation of config options, but I don't think we can make an assumption about peoples' setups here.

jams2 avatar Oct 08 '24 21:10 jams2

Closing as v0.8.0 actually fixes this in my project. The change to using Site.find_for_request means we'll fall back to the preview URL of the default site.

jams2 avatar Oct 28 '24 17:10 jams2