Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from .templates import TemplateView
6# Exports
8__all__ = (
9 "ImageView",
10)
12# Views
15class ImageView(TemplateView):
16 """Generic view for displaying images in full-screen.
18 **Configuration**
20 Add ``superdjango.assets.apps.DefaultConfig`` to ``INSTALLED_APPS``.
22 **Usage**
24 Create a view that extends ``ImageView``:
26 .. code-block:: python
28 # views.py
29 from superdjango.ui.views import ImageView
31 class MyImage(ImageView):
32 pattern_name = "display_image"
33 pattern_value = "images/"
35 .. tip::
36 For more control over the template, set the ``template_name`` on your view. See the provided ``image_view.html``
37 for an example.
39 You may call your view in a template, passing the following parameters via GET:
41 - ``i`` (required): The URL of the image.
42 - ``t`` (optional): A title for the image.
43 - ``z`` (optional): Set to ``1`` to enable zoom. Default is ``0`` (disabled).
45 For example, assuming you've defined a model with an ``image`` field:
47 .. code-block:: html
49 # template.html
50 <img src="{{ object.image.url }}" class="thumbnail">
51 <p>
52 <a href="{% url "display_image" %}?i={{ object.image.url }}&t={{ object.title|urlencode }}" target="_blank">
53 {% trans "Full Screen" %}
54 </a>
55 </p>
57 The provided ``image_view.html`` template extends ``SUPERDJANAGO_BASE_TEMPLATE_NAME`` and expects the following
58 template code to exist in that template.
60 .. code-block:: html
62 {% if view_css %}
63 {% for html in view_css %}
64 {{ html }}
65 {% endfor %}
66 {% endif %}
68 {% if view_js %}
69 {% for html in view_js %}
70 {{ html }}
71 {% endfor %}
72 {% endif %}
74 """
75 template_name = "superdjango/views/image_view.html"
77 def get_context_data(self, **kwargs):
78 """Get context for viewing the image.
80 - ``image``: The image (URL) to be displayed.
81 - ``title``: A title to be displayed above the image.
82 - ``zoom_enabled``: Indicates whether zoom should be enabled for the image.
84 """
85 context = super().get_context_data(**kwargs)
87 default_image = "%superdjango/views/images/image-not-found.jpg" % settings.STATIC_URL
88 context['image'] = self.request.GET.get("i", default_image)
90 context['title'] = self.request.GET.get("t", "")
92 context['zoom_enabled'] = self.request.GET.get("z", False)
94 return context