Hide keyboard shortcuts

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 

2 

3from django.conf import settings 

4from .templates import TemplateView 

5 

6# Exports 

7 

8__all__ = ( 

9 "ImageView", 

10) 

11 

12# Views 

13 

14 

15class ImageView(TemplateView): 

16 """Generic view for displaying images in full-screen. 

17 

18 **Configuration** 

19 

20 Add ``superdjango.assets.apps.DefaultConfig`` to ``INSTALLED_APPS``. 

21 

22 **Usage** 

23 

24 Create a view that extends ``ImageView``: 

25 

26 .. code-block:: python 

27 

28 # views.py 

29 from superdjango.ui.views import ImageView 

30 

31 class MyImage(ImageView): 

32 pattern_name = "display_image" 

33 pattern_value = "images/" 

34 

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. 

38 

39 You may call your view in a template, passing the following parameters via GET: 

40 

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). 

44 

45 For example, assuming you've defined a model with an ``image`` field: 

46 

47 .. code-block:: html 

48 

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> 

56 

57 The provided ``image_view.html`` template extends ``SUPERDJANAGO_BASE_TEMPLATE_NAME`` and expects the following 

58 template code to exist in that template. 

59 

60 .. code-block:: html 

61 

62 {% if view_css %} 

63 {% for html in view_css %} 

64 {{ html }} 

65 {% endfor %} 

66 {% endif %} 

67 

68 {% if view_js %} 

69 {% for html in view_js %} 

70 {{ html }} 

71 {% endfor %} 

72 {% endif %} 

73 

74 """ 

75 template_name = "superdjango/views/image_view.html" 

76 

77 def get_context_data(self, **kwargs): 

78 """Get context for viewing the image. 

79 

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. 

83 

84 """ 

85 context = super().get_context_data(**kwargs) 

86 

87 default_image = "%superdjango/views/images/image-not-found.jpg" % settings.STATIC_URL 

88 context['image'] = self.request.GET.get("i", default_image) 

89 

90 context['title'] = self.request.GET.get("t", "") 

91 

92 context['zoom_enabled'] = self.request.GET.get("z", False) 

93 

94 return context