1

I am trying to upload video files to vimeo, this is my code views.py

def upload_to_vimeo(request):
    token = 'xxxxx'
    if request.method == 'POST':
        video_file = request.POST.get("video_file")
        v = vimeo.VimeoClient(
            token=token,
        )
        video_uri = v.upload(video_file)
        print(video_uri)
        return redirect('home')
    return render(request, 'forms/video_upload_form.html', {})

template

                <form action="." method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                    <input type="file">
                    <input type="submit" class="button" value="Upload">
                </form>

but it throws error

AttributeError at /upload_to_vimeo/
'NoneType' object has no attribute 'read'

but I can directly upload without a form as follows

 v = vimeo.VimeoClient(
        token=token,
    )
    video_uri = v.upload('client_admin/test1.mp4')

How I can do it with a form, any help

Edit

Traceback:

File "/home/biju/Desktop/Dev/multitenant/lib/python3.8/site-packages/vimeo/upload.py" in __get_file_size
  193.             return os.path.getsize(filename)

File "/usr/lib/python3.8/genericpath.py" in getsize
  50.     return os.stat(filename).st_size

During handling of the above exception (stat: path should be string, bytes, os.PathLike or integer, not NoneType), another exception occurred:

File "/home/biju/Desktop/Dev/multitenant/lib/python3.8/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/biju/Desktop/Dev/multitenant/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/biju/Desktop/Dev/multitenant/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/biju/Documents/mtenant/client_admin/views.py" in upload_to_vimeo
  773.         video_uri = v.upload(video_file)

File "/home/biju/Desktop/Dev/multitenant/lib/python3.8/site-packages/vimeo/upload.py" in upload
  43.         filesize = self.__get_file_size(filename)

File "/home/biju/Desktop/Dev/multitenant/lib/python3.8/site-packages/vimeo/upload.py" in __get_file_size
  195.             return len(filename.read())

Exception Type: AttributeError at /upload_to_vimeo/
Exception Value: 'NoneType' object has no attribute 'read'

This is the error traceback and urls file is as follows

path('upload_to_vimeo/', upload_to_vimeo, name='upload_to_vimeo'),
7
  • 1
    You'll get file in request.FILES not in request.POST. Commented Nov 25, 2020 at 3:35
  • I have changed to files as you said, but the error is same Commented Nov 25, 2020 at 3:41
  • @Biju, I guess that the user isn't uploading the file. Check my answer, and be sure to read through the docs for request.FILES Commented Nov 25, 2020 at 3:44
  • Can you show your urls.py and the complete traceback? Commented Nov 25, 2020 at 3:55
  • @SafwanSamsudeen, i have edited the question to include traceback and url Commented Nov 25, 2020 at 4:07

2 Answers 2

1

There are lots of things wrong with your code. Do all of the following, and check whether it works

Views

  • You aren't doing validation, so if the video_file is None, the error you keep saying will come
  • You should get the file from request.FILES
def upload_to_vimeo(request):
    token = 'xxxxx'
    if request.method == 'POST':
        video_file = request.FILES.get("video_file") # Returns None if there is no file
        if not video_file: # If the user hasn't uploaded the file, 
            return redirect('some-error-page') # redirect to some error page or to the same page with an error message. Or do something else
        v = vimeo.VimeoClient(
            token=token,
        )
        video_uri = v.upload(video_file)
        return redirect('home')
    return render(request, 'forms/video_upload_form.html', {})

HTML

  • You aren't setting the name attribute to the file.
  • I'm not sure about this, but I don't think you are supposed to put a dot in the action attribute (correct me in the comments if I'm wrong). Setting the action attribute to a blank string will POST it to the same page.
<form action="" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="video_file">
    <input type="submit" class="button" value="Upload">
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer, even if i change to request.files the error is same, my doubt is it actually a post request?
0

From what I can see you're omitting the name attribute for your file input field, hence request.POST.get("video_file") returns none.

Add name to your file input field name="video_file"

<form action="" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="video_file">
    <input type="submit" class="button" value="Upload">
</form>

UPDATE

update your view

video_file = request.FILES['video_file']

5 Comments

@crazychukz, it is a valid point, but still the error is there, 'NoneType' object has no attribute 'read'
@Biju I have updated the answer. It should get the file now. I tested locally myself
now it throws another error Exception Type: AttributeError at /upload_to_vimeo/ Exception Value: 'TypeError' object has no attribute 'message'
@Biju from the code you've so far I can't see what could be causing this new error... I can't see any reference to the message variable or where you tried accessing it
Thank you guys, I have got answer from this question, stackoverflow.com/questions/64999871/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.