3

Can I access content from Mezzanine page gallery in a template I use for another page?

For example I have a gallery page that shows a collection of images I have added in Django Admin to the "Media Library". The page works fine and shows all of the images I have selected for the page.

The gallery page template has some code for displaying the images that looks something like...

{% 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 %}

However, on a different page, I want to use those same images, in the same sequence within a list I'll use to drive a jQuery slideshow.

Is there a way to use a template tag something like '{% with page.gallery.images.all as images %}' but make it point to the specific page that has the gallery images I want?

Thanks in advance for any insight you can provide.

2 Answers2

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
  • 906
  • 8
  • 14
  • Thank you for a response! Looking at your suggested code, won't this loop through ALL the pages (even those that aren't gallery pages) and process any gallery object on each? How do you specify the specific gallery page that has the gallery on it? In my case the slug to the page I want is '/gallery/sar'. – Andrew Anderson May 16 '13 at 05:31
  • Updated to use specifically Gallery model. You can also create template tag. I can show example. – MechanisM May 16 '13 at 05:49
  • THANK YOU! I edited your snippet and added the if statement so I would only process a specific gallery page of images (gallery/sar). Without the if statement your snippet returned ALL images from ALL gallery pages. Thanks again. I am learning django, mezzanine, and python. Your example helped a lot. – Andrew Anderson May 16 '13 at 22:57
  • Hmm... It looks like my edit of your snippet weren't accepted so... I added '{% if page.slug == 'gallery/sar' %}' on a line below your '{% for page in pages %}' line. I also added the corresponding '{% endif %}' on a line before your last '{% endfor %}'. That limits the snippet to processing one specific gallery page. – Andrew Anderson May 16 '13 at 23:59
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
  • 906
  • 8
  • 14