Django rest framework download file






















This is a little snippet of code, where a user can download the file by clicking a link of target blank. The web app is running on Nuxtjs server localhost and Django server is running on localhost, only the API is used to communicate between the Nuxtjs and Django by using the auth token.

When I click the download link it opens up a new tab and make a request from that new tab, where no token is passed with the request. Is there any other way that I can make it work to download the file by checking whether the requested user is the Ticket's owner? Because you are using JWT, you should download the file from your frontend after requesting it from your api using some sort of ajax request and the JWT header.

I'm using this answer as an example. Your API should not be changed. The way you did it is the way to go. I asked a question like that a few years ago. You might find some help there. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. Download a file using Django rest framework by authenticating the user Ask Question. Asked 3 years, 8 months ago. Active 3 years, 8 months ago. Project links Homepage. Maintainers evansmurithi. Overview REST framework files allows you to download a file in the format used to render the response and also allows creation of model instances by uploading a file containing the model fields.

Requirements Python 2. Installation Install using pip : pip install djangorestframework-files. Example models. ImportExportRouter router. For sample file examples you can upload, check the assets folder For more examples on how to use the viewsets or generic views, check the test application.

Project details Project links Homepage. Download files Download the file for your platform. After saving the model we are going to call all the objects from the model and render to the index2 html page. HTML :- Here in this file we had created a form with method equals to post and specify the action or the URL to relocate after the user clicks on the submit button.

HTML:- Here, this file is loaded when the user clicks the submit button in the index. Here we passed all the objects of our model which specifies the title and URL of the uploaded file. If you focus on the anchor tag, I had passed the URL of the file in HREF and specified the keyword download inside it, so that when the user clicks the link instead of redirecting to a new page, it will download the file.

I hope this blog will be useful for you and helps you to understand each and every step. Hope all your doubts get cleared. Best of Luck and Thank you.



0コメント

  • 1000 / 1000