【问题标题】:Masonite - AttributeError > 'str' object has no attribute 'filename'Masonite - AttributeError > 'str' 对象没有属性 'filename'
【发布时间】:2019-05-10 05:22:52
【问题描述】:

我有一个如下所示的表单:

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

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

但是当我提交此表单并尝试上传时,我只得到了图片的名称:

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

我遇到了异常:

AttributeError &gt; 'str' object has no attribute 'filename'

【问题讨论】:

    标签: python frameworks masonite


    【解决方案1】:

    这是因为您没有在此处的 HTML 表单上设置编码:

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

    这应该改为:

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

    这将对图像进行编码,以便 Masonite 可以将其作为对象而非字符串读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 2021-10-04
      • 2019-12-02
      • 2021-09-25
      • 2014-03-04
      • 2013-09-22
      相关资源
      最近更新 更多