Django-cms: how to get a page from its associated application

Given this apphook:

class NewsHook(CMSApp):
    name = _("News")
    urls = ["apps.news.urls"]

apphook_pool.register(NewsHook)

and this model is inside apps.news.models:

class Article(models.Model):
    title = models.CharField(max_length=255)
    ...

Can I link to a page related to apphook, for example, in an article method?

From the side of the model I came to article._meta.app_labelor article._meta.app_config.verbose_name, but it gives only 'news'and 'news'accordingly.

And I know from https://github.com/divio/django-cms/blob/7888ab8421bb836c8f7a1127d9a2bf4d4bbdf23e/cms/models/pagemodel.py#L82 that the page apphook is available with the help page.application_urlsthat gives me 'u'NewsHook'.

But I do not have enough links.

I believe that I could filter the pages by the application_urls field and look for a match with mine article._meta.app_config.verbose_name, but that would be neither a failure nor a beautiful one.

Any ideas for a better way?

+4
1

, - , , , , , , .

from applications.cms_apps import ApplicationAppHook
from cms.models.pagemodel import Page

class Application(models.Model):
    def related_cms_page(self):
        return Page.objects.filter(application_namespace=ApplicationAppHook.app_name).public().first()

templatetag, application_namespace

from cms.models.pagemodel import Page

@register.assignment_tag()
def get_page_by_namespace(application_namespace_str):
    try:
        return Page.objects.filter(application_namespace=application_namespace_str).public().first()
    except AttributeError:
        # EAFP ;)
        return None

:

{% get_page_by_namespace 'applications_apphook' as page %}
{% if page %}
    {{ page.get_menu_title }}
    {# Official Django CMS templatetags also works in this instance, i.e. {% page_attribute "page_title" page %} but it seems a bit redundant to me #}
{% endif %}
0

Source: https://habr.com/ru/post/1628360/


All Articles