Search code examples
pythonframeworksmasonite

Masonite - AttributeError > 'str' object has no attribute 'filename'


I have a form that looks like this:

<form method="POST" action="/posts">
    {{ csrf_field }}
    <input type="text" name="username">
    <input type="file" name="image">

    <input type="submit" value="Submit">
</form>

But when I submit this form and try to upload I am only getting the name of the image:

def posts(self, request: Request, upload: Upload):
    upload.store(request().input('image'))

I get hit with an exception:

AttributeError > 'str' object has no attribute 'filename'


Solution

  • This is thrown because you do not have an encoding set on your HTML form here:

    <form method="POST" action="/posts">
    

    This should be changed to:

    <form method="POST" action="/posts" enctype="multipart/form-data">
    

    This will encode the image so Masonite can read it as an object and not a string.