Lalas M
Lalas M

Reputation: 1174

How to return the current page url inside a wagtail app's model.py file

I have a wagtail app in which I need to return the current page's url. The code for my model.py of my app is as follows

import urllib

from django.conf import settings
from django.db import models

from wagtail.core.models import Page
from django.http import HttpResponse

class PageUrl(Page):
    def returnPageUrl(self):
        page_url = """ i need the page url here """
        return page_url

Upvotes: 3

Views: 4077

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

A Page model has a field url_path. You thus can access the page URL with:

class PageUrl(Page):

    def returnPageUrl(self):
        return self.url_path

or you can make use of full_url [wagtail-doc] to obtain the full url (including the hostname, etc.):

class PageUrl(Page):

    def returnPageUrl(self):
        return self.full_url

That being said, since it is already a field, I think there is not much use to define an extra method for this.

Upvotes: 8

Related Questions