Reputation: 1
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
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