I'm working on web application in which user upload a video and we perform speech recognition on it and then translate its text in another language.So, if the request is POST then we call that function which perform these functionalities and if request is GET it should not call that but in my case,on POST request, function execute first and then POST request is submitted.And it perform functionality on the last video in database.
It gives the same result even if I put function call at the end of POST request condition statement and if I put function call out of POST request condition the function call on every reload of page which is not required and I don't know other ways how to solve this problem.
"""Django==2.0.2 views.py"""
def generatingsubtitle(request):
latestvideo = Video.objects.last()
videofile = latestvideo.videofile
path = settings.MEDIA_ROOT + videofile.name
filename = os.path.splitext(videofile.name)[0]
srtpath=settings.STATIC_ROOT+filename
if request.method == 'POST':
#function call
main(path, srtpath)
form = forms.VideoForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('generatingsubtitle'),
)
else:
form = VideoForm()
return render(request, 'app/generate.html', {
'form': form,
'videofile': videofile,
'filename':filename,
},
)
I expect that ,POST request is submitted first and we take path of newly submitted file and pass it to the function call but the actual output is, first call the function after executing function POST request is submitted.
Any help will be highly appreciated.
You should be only be doing your processing when the form is valid and the model is saved. The way you have it, you're fetching the last model before the form is even saved, meaning you're acting on the previous model, not the current one.
Move your subtitle function inside the form.is_valid()
check, then get the path to the saved file from the saved model and feed that to your processing function.
def generatingsubtitle(request):
if request.method == 'POST':
form = forms.VideoForm(request.POST or None, request.FILES or None)
if form.is_valid():
# first save the model
video_model = form.save()
# get the path of the saved file from the model
video_path = video_model.videofile.path
# this may not be necessary anymore?
[filename, ext] = os.path.splitext(video_model.videofile.name)
srtpath = settings.STATIC_ROOT + filename
# generate subtitles
main(video_path, srtpath)
return HttpResponseRedirect(reverse('generatingsubtitle'))
You should not querying the database unless the method is 'POST'. Try this.
def generatingsubtitle(request):
filename = os.path.splitext(videofile.name)[0]
videofile = latestvideo.videofile
if request.method == 'POST':
form = forms.VideoForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
latestvideo = Video.objects.last()
path = settings.MEDIA_ROOT + videofile.name
srtpath=settings.STATIC_ROOT+filename
#function call
main(path, srtpath)
return HttpResponseRedirect(reverse('generatingsubtitle'))
else:
form = VideoForm() #edit
context = {
'form': form,
'videofile': videofile,
'filename':filename,
}
return render(request, 'app/generate.html', context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.