djangourlwagtailwagtail-pageurl

Get absolute or relative url of wagtail page


I have to following code snippet which should return a correct url (relative or absolute)

class LinkFields(models.Model):
    link_external   = models.URLField("External link", blank=True)
    link_page       = models.ForeignKey('wagtailcore.Page', null=True, blank=True, related_name='+')
    link_document   = models.ForeignKey('wagtaildocs.Document', null=True, blank=True, related_name='+' )

    @property
    def url(self):
        if self.link_page:
            return self.link_page.url
        elif self.link_document:
            return self.link_document.url
        else:
            return self.link_external

    panels = [
        FieldPanel('link_external'),
        PageChooserPanel('link_page'),
        DocumentChooserPanel('link_document'),
    ]

    class Meta:
        abstract = True

So if I use the ".url" property for a "wagtailcore.Page" a get an absolute url starting with "http" instead of "https".

What is the correct wagtail way to return a correct relative url or correct absolute url (leading with https in my case) within my "view/model"?

Thank you


Solution

  • Wagtail should do that out of the box provided the base URL for the Site model is HTTPS.

    There's a mailing list thread for this. https://groups.google.com/forum/#!topic/wagtail/atUeMXcNoCc