user3546241
user3546241

Reputation: 1

Django: how to download file from a given url from user?

I am trying to let user provide a url and django will download automatically but have no idea what to do. Thanks in advance.

Upvotes: 0

Views: 592

Answers (2)

bgschiller
bgschiller

Reputation: 2127

You can retrieve a url using the requests library.

To get the URL from the user, you can use a form.

Then, in the view, you can do something like

if request.method == 'POST': # If the form has been submitted...

    form = URLForm(request.POST) # A form bound to the POST data
    if form.is_valid(): # All validation rules pass
        response = requests.get(form.entered_url,stream=True)
        with open('save_file','w') as f:
            for chunk in response.iter_content():
                f.write(chunk)
        return HttpResponseRedirect('/thanks/') # Redirect after POST

Upvotes: 1

yorodm
yorodm

Reputation: 4461

My advice is you use celery and request to asynchroniusly download the files. There's a site that explains thoroughly how to integrate Django and Celery

Upvotes: 1

Related Questions