# Imports
from django.conf import settings
from .templates import TemplateView
# Exports
__all__ = (
"ImageView",
)
# Views
[docs]class ImageView(TemplateView):
"""Generic view for displaying images in full-screen.
**Configuration**
Add ``superdjango.assets.apps.DefaultConfig`` to ``INSTALLED_APPS``.
**Usage**
Create a view that extends ``ImageView``:
.. code-block:: python
# views.py
from superdjango.ui.views import ImageView
class MyImage(ImageView):
pattern_name = "display_image"
pattern_value = "images/"
.. tip::
For more control over the template, set the ``template_name`` on your view. See the provided ``image_view.html``
for an example.
You may call your view in a template, passing the following parameters via GET:
- ``i`` (required): The URL of the image.
- ``t`` (optional): A title for the image.
- ``z`` (optional): Set to ``1`` to enable zoom. Default is ``0`` (disabled).
For example, assuming you've defined a model with an ``image`` field:
.. code-block:: html
# template.html
<img src="{{ object.image.url }}" class="thumbnail">
<p>
<a href="{% url "display_image" %}?i={{ object.image.url }}&t={{ object.title|urlencode }}" target="_blank">
{% trans "Full Screen" %}
</a>
</p>
The provided ``image_view.html`` template extends ``SUPERDJANAGO_BASE_TEMPLATE_NAME`` and expects the following
template code to exist in that template.
.. code-block:: html
{% if view_css %}
{% for html in view_css %}
{{ html }}
{% endfor %}
{% endif %}
{% if view_js %}
{% for html in view_js %}
{{ html }}
{% endfor %}
{% endif %}
"""
template_name = "superdjango/views/image_view.html"
[docs] def get_context_data(self, **kwargs):
"""Get context for viewing the image.
- ``image``: The image (URL) to be displayed.
- ``title``: A title to be displayed above the image.
- ``zoom_enabled``: Indicates whether zoom should be enabled for the image.
"""
context = super().get_context_data(**kwargs)
default_image = "%superdjango/views/images/image-not-found.jpg" % settings.STATIC_URL
context['image'] = self.request.GET.get("i", default_image)
context['title'] = self.request.GET.get("t", "")
context['zoom_enabled'] = self.request.GET.get("z", False)
return context