Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast what if you could invest in your favorite developer? Who owns this outage? Building intelligent escalation chains for modern SRE. Featured on Meta. Now live: A fully responsive profile. Add a comment. Active Oldest Votes. Bhautik Domadiya Bhautik Domadiya 84 1 1 silver badge 6 6 bronze badges. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The urlretrieve function in the standard library is great if you only need to download a static file and if you don't need to do anything funny with the request. If you need to craft a special request for example set headers, cookies, query params and so on , you may want to implement the same thing using the awesome requests library.
Here, we're creating a temporary file using TemporaryFile from the standard library. We're using it as a context processor, so it'll automatically clean up delete temporary file after the block of the code is exited.
We then make a GET request with requests in streaming mode, so it doesn't try to download the entire file in memory. Instead it gives us chunks, which we save to a temporary file. This approach avoids causing problems if the files to download are big relative to available memory you never know when your user might attempt to set a two-hour full-HD movie as their avatar picture.
The urlretrieve function also works in the same way. Save Article. Improve Article. Like Article. Model :. Application definition. Generated by Django 2. Migration :. FileField ,. Previous FileField - Django forms. Next ImageField - Django Models. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.
0コメント