我想让用户提供一个网址,django会自动下载但不知道该怎么做。提前谢谢。
答案 0 :(得分:1)
您可以使用requests library检索网址。
要从用户获取网址,您可以使用form。
然后,在视图中,您可以执行类似
的操作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
答案 1 :(得分:1)