Need a user to upload videos to the site with AJAX.
Here are my files:

HTML:

<form method="post" enctype="multipart/form-data" style="display: none;" id="addvideoform"> {% csrf_token %} <p class="erroraddvideo"></p> <input placeholder="Имя" autocomplete="off" id="addtitlevideo" name="title" type="text"> <p class="poskimgvideo">Изображение</p> <input placeholder="img" id="addimagevideo" name="image" type="file"> <p class="poskimgvideo">Видео</p> <input id="addvideovideo" name="video" type="file"> <textarea placeholder="Описание" wrap="soft | hard" autocomplete="off" id="addopisaniyevideo" name="opisaniye"></textarea> <button id="addvideobutton" type="submit">Добавить</button> </form> 

urls.py:

 path('kabinet/addvideo/', addvideo, name='addvideo'), 

views.py:

 def addvideo(request): if request.method == "POST": title = request.POST.get('title', None) opisaniye = request.POST.get('opisaniye', None) img = request.FILES['img'] video = request.FILES['video'] user = request.user.get_full_name VideoBlog.objects.create( user=request.usesr, title=title, imgtitle=img, opisaniye=opisaniye, video=video, videotype='video' ) return JsonResponse({'title': title, 'opisaniye': opisaniye, 'img':img, 'video':video, 'user':user}) 

Javascript

  $('#addvideoform').on('submit', function(event){ event.preventDefault(); $('.erroraddvideo').val(''); var title = $('#addtitlevideo').val() var opisaniye = $('#opisaniye').val() var img = $('#addimagevideo'); var video = $('#addvideovideo'); $.ajax({ url: '/accounts/kabinet/addvideo/', type: 'POST', data: { 'title': title, 'opisaniye': opisaniye, 'img': img.value, 'video': video.value }, success : function(json){ alert('Ok') }, error : function(){ alert('No') } }); }); 

Closed due to the fact that off-topic participants jfs , 0xdb , Edward , Kosta B. , kot-da-vinci Jul 26 '18 at 6:17 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - jfs, 0xdb, Edward, Kosta B., kot-da-vinci
If the question can be reformulated according to the rules set out in the certificate , edit it .

    0