Как загрузить видео в пользовательской машине Django
Вот мой файл views.py
from django.shortcuts import render,HttpResponse,redirect
import youtube_dl
from django.contrib import messages
from pytube import *
# Create your views here.
def home(request):
return render(request,'home.html')
def download(request):
if request.method == 'POST':
video_url = request.POST.get('url')
if video_url:
ydl_opts = {'outtmp1': 'D:/'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
messages.success(request, 'Video Downloaded.')
return redirect('home')
else:
messages.warning(request, 'Please Enter Video URL')
return redirect('home')
return redirect('home')
Как я могу загрузить видео файл на машину пользователя? кто-нибудь может мне помочь?
Из вашей программы следует, что сгенерированная ссылка не возвращается в home.html
Просто как приведенный ниже код
def some_view(request):
context = {}
load_template = "home.html"
context['segment'] = load_template
context['url'] = {YOUR_URL}
html_template = loader.get_template( load_template )
return HttpResponse(html_template.render(context, request))
Кроме того, необходимо сначала сохранить сгенерированный файл в пространстве, к которому может получить доступ пользователь, а затем вернуть URL файла пользователю
from django.http import HttpResponse
from wsgiref.util import FileWrapper
def download_pdf(request):
filename = 'whatever_in_absolute_path__or_not.pdf'
content = FileWrapper(filename)
response = HttpResponse(content, content_type='application/pdf')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'whatever_name_will_appear_in_download.pdf'
return response
Вы можете использовать его для файлов любого типа.