Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a mezzanine Page with a given slug into the base template?

I am trying to include a page editable through the mezzanine admin in all the pages on my site. I read through the Mezzanine doc and the source and cannot figure out how to do this.

From the docs, I thought I could pass my page as an extra context, something like:

mezzanine.pages.views.page(request, slug, template=u'pages/page.html', extra_context={'mypage':<get_page_by_its_slug>})

But the doc says that the extra context is a mezzanine.pages.middleware.PageMiddleware object, which sets the slug from the request.

Do I need to write a context processor to do this? How do a load a specific page by its slug?

like image 286
user1255933 Avatar asked Feb 17 '23 12:02

user1255933


1 Answers

Just in case this helps someone out there, I created a context processor to solve this:

# context_processors.py
from mezzanine.pages.models import Page

def featured(request):
    # editable page, get by ID or slug or title...
    featured_page = Page.objects.get(id=49)
    return {'featured_page': featured_page}

added the context processor into my settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
   "django.contrib.auth.context_processors.auth",
   ## ...etc...,
   "myapp.context_processors.featured",
)

and included the featured content in the base.html template:

{% block right_panel %}
<div>
    {% editable featured_page.richtextpage.content %}
        {{ featured_page.richtextpage.content|richtext_filter|safe }}
        {% endeditable %}
</div>
{% endblock %}

If you know of a simpler way to do this, I'd love to hear your solution!

like image 116
user1255933 Avatar answered May 13 '23 04:05

user1255933