Skip to content

Instantly share code, notes, and snippets.

@alej0varas
Created September 26, 2014 12:41
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alej0varas/e7e334643ceab6e65744 to your computer and use it in GitHub Desktop.
Save alej0varas/e7e334643ceab6e65744 to your computer and use it in GitHub Desktop.
Wagtail redirect page. Can link to page, ulr and document.
# Thanks to Ethan Jucovy
# http://www.coactivate.org/projects/ejucovy/blog/2014/05/10/wagtail-notes-managing-redirects-as-pages/
# This model comes from wagtaildemo
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 link(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
# This is the model you are looking for
# from wagtail.wagtailcore.models import Page
class PageAlias(Page, LinkFields):
def serve(self, request):
return redirect(self.link, permanent=False)
PageAlias.content_panels = [FieldPanel('title')] + LinkFields.panels
@ttamg
Copy link

ttamg commented Sep 30, 2020

This is exactly what I was after. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment