AttributeError when page not in site
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.
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 🤔
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 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.
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.