Default setup for media in Django-Cookie cutter and relative media urls










0















I've used Django-Cookiecutter's default setup for Heroku, with Whitenoise serving static files and S3 serving media files.



However, I want to use 'media' to store certain, heavier content files (pdfs) that are going to change over time, not just user file uploads, and a change shouldn't require a collectstatic. (Eventually I'll create an authoring tool to upload files to S3, but for the moment I'm just manually placing some files like pdfs in correct S3 bucket subdirectories.)



The standard setup for Django-Cookiecutter is to set the MEDIA_URL to the full S3 address, like https://my-bucket.s3.amazonaws.com/media/some.pdf ... but I want to access these files in my templates using relative path like /media/some.pdf in the same way I can browse directly to static content via /static.



I want a relative URL like /media/some.pdf because I'm using the download attribute on the href tag, which should download the file when clicked, but with a fully qualified url pointing at amazonaws.com, the browser will ignore the download attribute and just load the file in the browser.



Is there a straightforward way to config Django (running on Heroku using Whitenoise for static and S3 for media) to return the file from S3 when accessed via /media?










share|improve this question


























    0















    I've used Django-Cookiecutter's default setup for Heroku, with Whitenoise serving static files and S3 serving media files.



    However, I want to use 'media' to store certain, heavier content files (pdfs) that are going to change over time, not just user file uploads, and a change shouldn't require a collectstatic. (Eventually I'll create an authoring tool to upload files to S3, but for the moment I'm just manually placing some files like pdfs in correct S3 bucket subdirectories.)



    The standard setup for Django-Cookiecutter is to set the MEDIA_URL to the full S3 address, like https://my-bucket.s3.amazonaws.com/media/some.pdf ... but I want to access these files in my templates using relative path like /media/some.pdf in the same way I can browse directly to static content via /static.



    I want a relative URL like /media/some.pdf because I'm using the download attribute on the href tag, which should download the file when clicked, but with a fully qualified url pointing at amazonaws.com, the browser will ignore the download attribute and just load the file in the browser.



    Is there a straightforward way to config Django (running on Heroku using Whitenoise for static and S3 for media) to return the file from S3 when accessed via /media?










    share|improve this question
























      0












      0








      0








      I've used Django-Cookiecutter's default setup for Heroku, with Whitenoise serving static files and S3 serving media files.



      However, I want to use 'media' to store certain, heavier content files (pdfs) that are going to change over time, not just user file uploads, and a change shouldn't require a collectstatic. (Eventually I'll create an authoring tool to upload files to S3, but for the moment I'm just manually placing some files like pdfs in correct S3 bucket subdirectories.)



      The standard setup for Django-Cookiecutter is to set the MEDIA_URL to the full S3 address, like https://my-bucket.s3.amazonaws.com/media/some.pdf ... but I want to access these files in my templates using relative path like /media/some.pdf in the same way I can browse directly to static content via /static.



      I want a relative URL like /media/some.pdf because I'm using the download attribute on the href tag, which should download the file when clicked, but with a fully qualified url pointing at amazonaws.com, the browser will ignore the download attribute and just load the file in the browser.



      Is there a straightforward way to config Django (running on Heroku using Whitenoise for static and S3 for media) to return the file from S3 when accessed via /media?










      share|improve this question














      I've used Django-Cookiecutter's default setup for Heroku, with Whitenoise serving static files and S3 serving media files.



      However, I want to use 'media' to store certain, heavier content files (pdfs) that are going to change over time, not just user file uploads, and a change shouldn't require a collectstatic. (Eventually I'll create an authoring tool to upload files to S3, but for the moment I'm just manually placing some files like pdfs in correct S3 bucket subdirectories.)



      The standard setup for Django-Cookiecutter is to set the MEDIA_URL to the full S3 address, like https://my-bucket.s3.amazonaws.com/media/some.pdf ... but I want to access these files in my templates using relative path like /media/some.pdf in the same way I can browse directly to static content via /static.



      I want a relative URL like /media/some.pdf because I'm using the download attribute on the href tag, which should download the file when clicked, but with a fully qualified url pointing at amazonaws.com, the browser will ignore the download attribute and just load the file in the browser.



      Is there a straightforward way to config Django (running on Heroku using Whitenoise for static and S3 for media) to return the file from S3 when accessed via /media?







      cookiecutter-django






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 23:18









      DanielMcQDanielMcQ

      155




      155






















          1 Answer
          1






          active

          oldest

          votes


















          0














          So you want to the browser to open a "download file" pop-up when the user click on the link? The download attribute isn't working because it only works for same-origin, according to the specs:




          This attribute only works for same-origin URLs.




          The specs also mentions that Content-Disposition header overrides it, so the header is a more robust way of doing this.



          You can set-up S3 to do that by setting the Content-Disposition header properly. Here is a running example below:






          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>





          When I uploaded the one with header, I've added the metadata header in the AWS console:



          Content-Disposition: attachment; filename="cookiecutter.pdf" 


          You cannot host your media files on Heroku, they will be lost at the next deploy due to their ephemeral filesystem, so having them on the server itself isn't an option.






          share|improve this answer

























          • Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

            – DanielMcQ
            Nov 25 '18 at 20:45












          • I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

            – Bruno A.
            Nov 26 '18 at 13:46











          • Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

            – Bruno A.
            Nov 26 '18 at 13:55










          Your Answer






          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: "1"
          ;
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310214%2fdefault-setup-for-media-in-django-cookie-cutter-and-relative-media-urls%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          So you want to the browser to open a "download file" pop-up when the user click on the link? The download attribute isn't working because it only works for same-origin, according to the specs:




          This attribute only works for same-origin URLs.




          The specs also mentions that Content-Disposition header overrides it, so the header is a more robust way of doing this.



          You can set-up S3 to do that by setting the Content-Disposition header properly. Here is a running example below:






          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>





          When I uploaded the one with header, I've added the metadata header in the AWS console:



          Content-Disposition: attachment; filename="cookiecutter.pdf" 


          You cannot host your media files on Heroku, they will be lost at the next deploy due to their ephemeral filesystem, so having them on the server itself isn't an option.






          share|improve this answer

























          • Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

            – DanielMcQ
            Nov 25 '18 at 20:45












          • I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

            – Bruno A.
            Nov 26 '18 at 13:46











          • Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

            – Bruno A.
            Nov 26 '18 at 13:55















          0














          So you want to the browser to open a "download file" pop-up when the user click on the link? The download attribute isn't working because it only works for same-origin, according to the specs:




          This attribute only works for same-origin URLs.




          The specs also mentions that Content-Disposition header overrides it, so the header is a more robust way of doing this.



          You can set-up S3 to do that by setting the Content-Disposition header properly. Here is a running example below:






          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>





          When I uploaded the one with header, I've added the metadata header in the AWS console:



          Content-Disposition: attachment; filename="cookiecutter.pdf" 


          You cannot host your media files on Heroku, they will be lost at the next deploy due to their ephemeral filesystem, so having them on the server itself isn't an option.






          share|improve this answer

























          • Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

            – DanielMcQ
            Nov 25 '18 at 20:45












          • I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

            – Bruno A.
            Nov 26 '18 at 13:46











          • Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

            – Bruno A.
            Nov 26 '18 at 13:55













          0












          0








          0







          So you want to the browser to open a "download file" pop-up when the user click on the link? The download attribute isn't working because it only works for same-origin, according to the specs:




          This attribute only works for same-origin URLs.




          The specs also mentions that Content-Disposition header overrides it, so the header is a more robust way of doing this.



          You can set-up S3 to do that by setting the Content-Disposition header properly. Here is a running example below:






          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>





          When I uploaded the one with header, I've added the metadata header in the AWS console:



          Content-Disposition: attachment; filename="cookiecutter.pdf" 


          You cannot host your media files on Heroku, they will be lost at the next deploy due to their ephemeral filesystem, so having them on the server itself isn't an option.






          share|improve this answer















          So you want to the browser to open a "download file" pop-up when the user click on the link? The download attribute isn't working because it only works for same-origin, according to the specs:




          This attribute only works for same-origin URLs.




          The specs also mentions that Content-Disposition header overrides it, so the header is a more robust way of doing this.



          You can set-up S3 to do that by setting the Content-Disposition header properly. Here is a running example below:






          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>





          When I uploaded the one with header, I've added the metadata header in the AWS console:



          Content-Disposition: attachment; filename="cookiecutter.pdf" 


          You cannot host your media files on Heroku, they will be lost at the next deploy due to their ephemeral filesystem, so having them on the server itself isn't an option.






          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>





          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
          Document with header set
          </a>
          <br/>
          <a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
          Document without header
          </a>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 13:54

























          answered Nov 23 '18 at 10:52









          Bruno A.Bruno A.

          56157




          56157












          • Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

            – DanielMcQ
            Nov 25 '18 at 20:45












          • I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

            – Bruno A.
            Nov 26 '18 at 13:46











          • Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

            – Bruno A.
            Nov 26 '18 at 13:55

















          • Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

            – DanielMcQ
            Nov 25 '18 at 20:45












          • I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

            – Bruno A.
            Nov 26 '18 at 13:46











          • Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

            – Bruno A.
            Nov 26 '18 at 13:55
















          Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

          – DanielMcQ
          Nov 25 '18 at 20:45






          Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible?

          – DanielMcQ
          Nov 25 '18 at 20:45














          I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

          – Bruno A.
          Nov 26 '18 at 13:46





          I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file.

          – Bruno A.
          Nov 26 '18 at 13:46













          Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

          – Bruno A.
          Nov 26 '18 at 13:55





          Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it.

          – Bruno A.
          Nov 26 '18 at 13:55



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310214%2fdefault-setup-for-media-in-django-cookie-cutter-and-relative-media-urls%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党