A DRYer solution to processing responsive thumbnails in Django template files

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












In order to display the appropriate image for a set of responsive breakpoints, I have put built a Context Processor that simply passes some Dicts with breakpoint and image resolution information. I then loop through these Dicts in order to generate responsive thumbnails as well as build the relevant CSS media queries.



This solution works well for me now, but as I repeat this code on all template pages that need responsive imagery it becomes less and less DRY, yet I am not quite sure how I would abstract this functionality further.



How can I make this thumbnail/responsive breakpoint-generating functionality more DRY?



context_processors.py



def thumbsizes(request):
thumbsizes =
'screensizes': [
'xs': '',
'sm': '796px',
'md': '1024px',
'lg': '1216px',
'xl': '1408px',
],
'hero': [
'xs': '600x827',
'sm': '900x400',
'md': '1200x400',
'lg': '1800x400',
'xl': '2400x400',
],
'main': [
...
],
...

return "thumbsizes": thumbsizes


_hero.html



% with object.image as image %
% with class_name=".c-hero" %
<style type="text/css">
% for size in thumbsizes.hero %
% for key, value in size.items %
# Build thumbnail with value from size Dict #
% thumbnail image value crop upscale subject_location=image.subject_location as thumb %

# Do not apply media query for extra-small devices #
% if key == 'xs' %
class_name % if child_class % child_class % endif %
background-image: url( thumb.url);

% else %
% for screensize in thumbsizes.screensizes %
% for skey, svalue in screensize.items %
% if skey != 'xs' and key == skey %
@media screen and (min-width: svalue )
class_name % if child_class % child_class % endif %
background-image: url( thumb.url);


% endif %
% endfor %
% endfor %
% endif %
% endfor %
% endfor %
</style>
% endwith %
% endwith %

<div class="c-hero"></div>






share|improve this question

























    up vote
    1
    down vote

    favorite












    In order to display the appropriate image for a set of responsive breakpoints, I have put built a Context Processor that simply passes some Dicts with breakpoint and image resolution information. I then loop through these Dicts in order to generate responsive thumbnails as well as build the relevant CSS media queries.



    This solution works well for me now, but as I repeat this code on all template pages that need responsive imagery it becomes less and less DRY, yet I am not quite sure how I would abstract this functionality further.



    How can I make this thumbnail/responsive breakpoint-generating functionality more DRY?



    context_processors.py



    def thumbsizes(request):
    thumbsizes =
    'screensizes': [
    'xs': '',
    'sm': '796px',
    'md': '1024px',
    'lg': '1216px',
    'xl': '1408px',
    ],
    'hero': [
    'xs': '600x827',
    'sm': '900x400',
    'md': '1200x400',
    'lg': '1800x400',
    'xl': '2400x400',
    ],
    'main': [
    ...
    ],
    ...

    return "thumbsizes": thumbsizes


    _hero.html



    % with object.image as image %
    % with class_name=".c-hero" %
    <style type="text/css">
    % for size in thumbsizes.hero %
    % for key, value in size.items %
    # Build thumbnail with value from size Dict #
    % thumbnail image value crop upscale subject_location=image.subject_location as thumb %

    # Do not apply media query for extra-small devices #
    % if key == 'xs' %
    class_name % if child_class % child_class % endif %
    background-image: url( thumb.url);

    % else %
    % for screensize in thumbsizes.screensizes %
    % for skey, svalue in screensize.items %
    % if skey != 'xs' and key == skey %
    @media screen and (min-width: svalue )
    class_name % if child_class % child_class % endif %
    background-image: url( thumb.url);


    % endif %
    % endfor %
    % endfor %
    % endif %
    % endfor %
    % endfor %
    </style>
    % endwith %
    % endwith %

    <div class="c-hero"></div>






    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In order to display the appropriate image for a set of responsive breakpoints, I have put built a Context Processor that simply passes some Dicts with breakpoint and image resolution information. I then loop through these Dicts in order to generate responsive thumbnails as well as build the relevant CSS media queries.



      This solution works well for me now, but as I repeat this code on all template pages that need responsive imagery it becomes less and less DRY, yet I am not quite sure how I would abstract this functionality further.



      How can I make this thumbnail/responsive breakpoint-generating functionality more DRY?



      context_processors.py



      def thumbsizes(request):
      thumbsizes =
      'screensizes': [
      'xs': '',
      'sm': '796px',
      'md': '1024px',
      'lg': '1216px',
      'xl': '1408px',
      ],
      'hero': [
      'xs': '600x827',
      'sm': '900x400',
      'md': '1200x400',
      'lg': '1800x400',
      'xl': '2400x400',
      ],
      'main': [
      ...
      ],
      ...

      return "thumbsizes": thumbsizes


      _hero.html



      % with object.image as image %
      % with class_name=".c-hero" %
      <style type="text/css">
      % for size in thumbsizes.hero %
      % for key, value in size.items %
      # Build thumbnail with value from size Dict #
      % thumbnail image value crop upscale subject_location=image.subject_location as thumb %

      # Do not apply media query for extra-small devices #
      % if key == 'xs' %
      class_name % if child_class % child_class % endif %
      background-image: url( thumb.url);

      % else %
      % for screensize in thumbsizes.screensizes %
      % for skey, svalue in screensize.items %
      % if skey != 'xs' and key == skey %
      @media screen and (min-width: svalue )
      class_name % if child_class % child_class % endif %
      background-image: url( thumb.url);


      % endif %
      % endfor %
      % endfor %
      % endif %
      % endfor %
      % endfor %
      </style>
      % endwith %
      % endwith %

      <div class="c-hero"></div>






      share|improve this question











      In order to display the appropriate image for a set of responsive breakpoints, I have put built a Context Processor that simply passes some Dicts with breakpoint and image resolution information. I then loop through these Dicts in order to generate responsive thumbnails as well as build the relevant CSS media queries.



      This solution works well for me now, but as I repeat this code on all template pages that need responsive imagery it becomes less and less DRY, yet I am not quite sure how I would abstract this functionality further.



      How can I make this thumbnail/responsive breakpoint-generating functionality more DRY?



      context_processors.py



      def thumbsizes(request):
      thumbsizes =
      'screensizes': [
      'xs': '',
      'sm': '796px',
      'md': '1024px',
      'lg': '1216px',
      'xl': '1408px',
      ],
      'hero': [
      'xs': '600x827',
      'sm': '900x400',
      'md': '1200x400',
      'lg': '1800x400',
      'xl': '2400x400',
      ],
      'main': [
      ...
      ],
      ...

      return "thumbsizes": thumbsizes


      _hero.html



      % with object.image as image %
      % with class_name=".c-hero" %
      <style type="text/css">
      % for size in thumbsizes.hero %
      % for key, value in size.items %
      # Build thumbnail with value from size Dict #
      % thumbnail image value crop upscale subject_location=image.subject_location as thumb %

      # Do not apply media query for extra-small devices #
      % if key == 'xs' %
      class_name % if child_class % child_class % endif %
      background-image: url( thumb.url);

      % else %
      % for screensize in thumbsizes.screensizes %
      % for skey, svalue in screensize.items %
      % if skey != 'xs' and key == skey %
      @media screen and (min-width: svalue )
      class_name % if child_class % child_class % endif %
      background-image: url( thumb.url);


      % endif %
      % endfor %
      % endfor %
      % endif %
      % endfor %
      % endfor %
      </style>
      % endwith %
      % endwith %

      <div class="c-hero"></div>








      share|improve this question










      share|improve this question




      share|improve this question









      asked Jan 28 at 15:36









      Dluks

      61




      61

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186202%2fa-dryer-solution-to-processing-responsive-thumbnails-in-django-template-files%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186202%2fa-dryer-solution-to-processing-responsive-thumbnails-in-django-template-files%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?