2
0

image_serve_view.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.wagtailimages.views.serve import ServeView
  18. urlpatterns = [
  19. ...
  20. url(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. url(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. .. code-block:: python
  40. from django.core.urlresolvers import reverse
  41. from wagtail.wagtailimages.views.serve import generate_signature
  42. def generate_image_url(image, filter_spec):
  43. signature = generate_signature(image.id, filter_spec)
  44. url = reverse('wagtailimages_serve', args=(signature, image.id, filter_spec))
  45. # Append image's original filename to the URL (optional)
  46. url += image.file.name[len('original_images/'):]
  47. return url
  48. And here's an example of this being used in a view:
  49. .. code-block:: python
  50. def display_image(request, image_id):
  51. image = get_object_or_404(Image, id=image_id)
  52. return render(request, 'display_image.html', {
  53. 'image_url': generate_image_url(image, 'fill-100x100')
  54. })
  55. Image operations can be chained by joining them with a ``|`` character:
  56. .. code-block:: python
  57. return render(request, 'display_image.html', {
  58. 'image_url': generate_image_url(image, 'fill-100x100|jpegquality-40')
  59. })
  60. Advanced configuration
  61. ======================
  62. .. _image_serve_view_redirect_action:
  63. Making the view redirect instead of serve
  64. -----------------------------------------
  65. By default, the view will serve the image file directly. This behaviour can be
  66. changed to a 301 redirect instead which may be useful if you host your images
  67. externally.
  68. To enable this, pass ``action='redirect'`` into the ``ServeView.as_view()``
  69. method in your urls configuration:
  70. .. code-block:: python
  71. from wagtail.wagtailimages.views.serve import ServeView
  72. urlpatterns = [
  73. ...
  74. url(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', ServeView.as_view(action='redirect'), name='wagtailimages_serve'),
  75. ]
  76. .. _image_serve_view_sendfile:
  77. Integration with django-sendfile
  78. --------------------------------
  79. `django-sendfile`_ offloads the job of transferring the image data to the web
  80. server instead of serving it directly from the Django application. This could
  81. greatly reduce server load in situations where your site has many images being
  82. downloaded but you're unable to use a :ref:`caching_proxy` or a CDN.
  83. .. _django-sendfile: https://github.com/johnsensible/django-sendfile
  84. You firstly need to install and configure django-sendfile and configure your
  85. web server to use it. If you haven't done this already, please refer to the
  86. `installation docs <https://github.com/johnsensible/django-sendfile#django-sendfile>`_.
  87. To serve images with django-sendfile, you can use the ``SendFileView`` class.
  88. This view can be used out of the box:
  89. .. code-block:: python
  90. from wagtail.wagtailimages.views.serve import SendFileView
  91. urlpatterns = [
  92. ...
  93. url(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', SendFileView.as_view(), name='wagtailimages_serve'),
  94. ]
  95. You can customise it to override the backend defined in the ``SENDFILE_BACKEND``
  96. setting:
  97. .. code-block:: python
  98. from wagtail.wagtailimages.views.serve import SendFileView
  99. from project.sendfile_backends import MyCustomBackend
  100. class MySendFileView(SendFileView):
  101. backend = MyCustomBackend
  102. You can also customise it to serve private files. For example, if the only need
  103. is to be authenticated (e.g. for Django >= 1.9):
  104. .. code-block:: python
  105. from django.contrib.auth.mixins import LoginRequiredMixin
  106. from wagtail.wagtailimages.views.serve import SendFileView
  107. class PrivateSendFileView(LoginRequiredMixin, SendFileView):
  108. raise_exception = True