[Django]-How to access Django/Mezzanine gallery content in a different page template

3๐Ÿ‘

โœ…

you need to create context processor like:

def all_pages(request):
    from mezzanine.galleries.models import Gallery
    galleries = Gallery.objects.all()
    return {'pages': galleries}

then add it to your settings.py in TEMPLATE_CONTEXT_PROCESSORS

TEMPLATE_CONTEXT_PROCESSORS += (
    'path.to.our.just.created.context_processor.all_pages',
)

then in template:

{% load mezzanine_tags %}

<ul class="thumbnails gallery">
{% for page in pages %}
{% with page.gallery.images.all as images %}
{% for image in images %}
<li>
    <a class="thumbnail" rel="#image-{{ image.id }}" title="{{ image.description }}" href="{{ MEDIA_URL }}{{ image.file }}">
        <img class="image-overlay-thumb" src="{{ MEDIA_URL }}{% thumbnail image.file 75 75 %}">
    </a>
    <div id="image-{{ image.id }}" class="image-overlay" style="display:none;">
        <a href="#" class="image-overlay-prev">&larr;</a>
        <a href="#" class="image-overlay-next">&rarr;</a>
        <img class="image-overlay-full" src="{{ MEDIA_URL }}{% thumbnail image.file 0 600 %}"><br>
        <p>{{ image.description }}<br>{{ forloop.counter }} / {{ images|length }}</p>
    </div>
</li>
{% endfor %}
{% endwith %}
{% endfor %}
</ul>

Iโ€™m not so familiar with mezzanine, but it should work, you can pass context in view or other way and manipulate it.

๐Ÿ‘คMechanisM

2๐Ÿ‘

you can also use templatetag like this:

@register.simple_tag
def show_gallery_by_slug(slug):
    from mezzanine.galleries.models import Gallery
    gallery = Gallery.objects.filter(slug=slug)
    template = get_template("pages/images.html")
    c = Context({"gallery": gallery})
    return template.render(c)

and then in templates

{% load yourtags %}

{% show_gallery_by_slug "galleryslugishere" %}

you also need to create template to display with for image in gallery..

๐Ÿ‘คMechanisM

Leave a comment