gpt4 book ai didi

django - 如何在不同的页面模板中访问 Django/Mezzanine 库内容

转载 作者:行者123 更新时间:2023-12-02 04:22:24 26 4
gpt4 key购买 nike

我可以在用于其他页面的模板中访问夹层页面库中的内容吗?

例如,我有一个图库页面,其中显示了我在 Django Admin 中添加到“媒体库”的图像集合。该页面工作正常并显示我为该页面选择的所有图像。

图库页面模板有一些用于显示图像的代码,看起来像......

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

但是,在不同的页面上,我想在列表中以相同的顺序使用这些相同的图像,我将用它来驱动 jQuery 幻灯片。

有没有办法使用类似“{% with page.gallery.images.all as images %}”的模板标签,但使其指向具有我想要的图库图像的特定页面?

预先感谢您提供的任何见解。

最佳答案

您需要创建上下文处理器,例如:

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

然后将其添加到 TEMPLATE_CONTEXT_PROCESSORS 中的 settings.py

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

然后在模板中:

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

我对夹层不太熟悉,但它应该可以工作,您可以在 View 或其他方式中传递上下文并对其进行操作。

关于django - 如何在不同的页面模板中访问 Django/Mezzanine 库内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16578806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com