image_serve_view.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. .. _using_images_outside_wagtail:
  2. ========================
  3. Dynamic image serve view
  4. ========================
  5. In most cases, developers wanting to generate image renditions in Python should use the ``get_rendition()``
  6. method. See :ref:`image_renditions`.
  7. If you need to be able to generate image versions for an *external* system such as a blog or mobile app,
  8. Wagtail provides a view for dynamically generating renditions of images by calling a unique URL.
  9. The view takes an image id, filter spec and security signature in the URL. If
  10. these parameters are valid, it serves an image file matching that criteria.
  11. Like the ``{% image %}`` tag, the rendition is generated on the first call and
  12. subsequent calls are served from a cache.
  13. Setup
  14. =====
  15. Add an entry for the view into your URLs configuration:
  16. .. code-block:: python
  17. from wagtail.images.views.serve import ServeView
  18. urlpatterns = [
  19. ...
  20. re_path(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', ServeView.as_view(), name='wagtailimages_serve'),
  21. ...
  22. # Ensure that the wagtailimages_serve line appears above the default Wagtail page serving route
  23. re_path(r'', include(wagtail_urls)),
  24. ]
  25. Usage
  26. =====
  27. Image URL generator UI
  28. ----------------------
  29. When the dynamic serve view is enabled, an image URL generator in the admin
  30. interface becomes available automatically. This can be accessed through the edit
  31. page of any image by clicking the "URL generator" button on the right hand side.
  32. This interface allows editors to generate URLs to cropped versions of the image.
  33. Generating dynamic image URLs in Python
  34. ---------------------------------------
  35. Dynamic image URLs can also be generated using Python code and served to a
  36. client over an API or used directly in the template.
  37. One advantage of using dynamic image URLs in the template is that they do not
  38. block the initial response while rendering like the ``{% image %}`` tag does.
  39. The ``generate_image_url`` function in ``wagtail.images.views.serve`` is a convenience
  40. method to generate a dynamic image URL.
  41. Here's an example of this being used in a view:
  42. .. code-block:: python
  43. def display_image(request, image_id):
  44. image = get_object_or_404(Image, id=image_id)
  45. return render(request, 'display_image.html', {
  46. 'image_url': generate_image_url(image, 'fill-100x100')
  47. })
  48. Image operations can be chained by joining them with a ``|`` character:
  49. .. code-block:: python
  50. return render(request, 'display_image.html', {
  51. 'image_url': generate_image_url(image, 'fill-100x100|jpegquality-40')
  52. })
  53. In your templates:
  54. .. code-block:: html+django
  55. {% load wagtailimages_tags %}
  56. ...
  57. <!-- Get the url for the image scaled to a width of 400 pixels: -->
  58. {% image_url page.photo "width-400" %}
  59. <!-- Again, but this time as a square thumbnail: -->
  60. {% image_url page.photo "fill-100x100|jpegquality-40" %}
  61. <!-- This time using our custom image serve view: -->
  62. {% image_url page.photo "width-400" "mycustomview_serve" %}
  63. You can pass an optional view name that will be used to serve the image through. The default is ``wagtailimages_serve``
  64. Advanced configuration
  65. ======================
  66. .. _image_serve_view_redirect_action:
  67. Making the view redirect instead of serve
  68. -----------------------------------------
  69. By default, the view will serve the image file directly. This behaviour can be
  70. changed to a 301 redirect instead which may be useful if you host your images
  71. externally.
  72. To enable this, pass ``action='redirect'`` into the ``ServeView.as_view()``
  73. method in your urls configuration:
  74. .. code-block:: python
  75. from wagtail.images.views.serve import ServeView
  76. urlpatterns = [
  77. ...
  78. re_path(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', ServeView.as_view(action='redirect'), name='wagtailimages_serve'),
  79. ]
  80. .. _image_serve_view_sendfile:
  81. Integration with django-sendfile
  82. --------------------------------
  83. `django-sendfile`_ offloads the job of transferring the image data to the web
  84. server instead of serving it directly from the Django application. This could
  85. greatly reduce server load in situations where your site has many images being
  86. downloaded but you're unable to use a :ref:`caching_proxy` or a CDN.
  87. .. _django-sendfile: https://github.com/johnsensible/django-sendfile
  88. You firstly need to install and configure django-sendfile and configure your
  89. web server to use it. If you haven't done this already, please refer to the
  90. `installation docs <https://github.com/johnsensible/django-sendfile#django-sendfile>`_.
  91. To serve images with django-sendfile, you can use the ``SendFileView`` class.
  92. This view can be used out of the box:
  93. .. code-block:: python
  94. from wagtail.images.views.serve import SendFileView
  95. urlpatterns = [
  96. ...
  97. re_path(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', SendFileView.as_view(), name='wagtailimages_serve'),
  98. ]
  99. You can customise it to override the backend defined in the ``SENDFILE_BACKEND``
  100. setting:
  101. .. code-block:: python
  102. from wagtail.images.views.serve import SendFileView
  103. from project.sendfile_backends import MyCustomBackend
  104. class MySendFileView(SendFileView):
  105. backend = MyCustomBackend
  106. You can also customise it to serve private files. For example, if the only need
  107. is to be authenticated (e.g. for Django >= 1.9):
  108. .. code-block:: python
  109. from django.contrib.auth.mixins import LoginRequiredMixin
  110. from wagtail.images.views.serve import SendFileView
  111. class PrivateSendFileView(LoginRequiredMixin, SendFileView):
  112. raise_exception = True