我正在尝试从简单的HMTL表单上传图像文件,我收到以下错误:
'MultiValueDict'对象没有属性'_committed'
我没有以同样的方式使用表单原因我需要一次上传多个文件,而且我知道Djangoforms只允许一次上传一个文件。
所以使用正常的方式似乎不起作用......
代码结构如下:
的 Models.py
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<div>
<a class="brand" href="/{{locationService.getLangKey()}}" style="text-decoration: none"><img src="img/logo.png" /><span class="logo">yanpy</span></a>
</div>
</div>
<div class="span8 pull-right" style="padding-right: 10px">
<div class="navbar">
<div class="navbar-inner custom-navbar-inner">
<!-- Responsive Navbar Part 1: Button for triggering responsive navbar (not covered in tutorial). Include responsive CSS to utilize. -->
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Responsive Navbar Part 2: Place all navbar contents you want collapsed withing .navbar-collapse.collapse. -->
<div class="nav-collapse collapse" style="padding-top: 4px">
<!--<div ng-include src="'partials/login.html'"></div>-->
</div><!--/.nav-collapse -->
</div><!-- /.navbar-inner -->
</div><!-- /.navbar -->
</div> <!-- span8 -->
</div>
</div>
HTML
class ProfileImage(models.Model):
account_id = models.IntegerField(primary_key=True)
image = models.ImageField(upload_to='profile/%Y/%m/%d')
profile = models.CharField(max_length=512, blank=True)
description = models.CharField(max_length=255, blank=True)
class Meta:
managed = False
db_table = 'ProfileImage'
Views.py
<form id="fileinfo" name="fileinfo" action="profileimage" enctype="multipart/form-data" method="post" >
{% csrf_token %}
<table>
<tr>
<input type="file" size="60" id="myfile" name="myfile" multiple="false" value="Profile Picture"
accept="image/gif, image/jpeg, image/png">
</tr>
<tr>
<input type="submit" value="Upload" />
<div id="accresult" name="accresult"></div>
</tr>
</table>
</form>
参数正确到达但是当我尝试def profile_image(request):
profile_image = ProfileImage(account_id=request.session.get('memberid', False), image=request.FILES, profile='', description='')
try:
print 'Userid: ', profile_image.account_id
print 'File sent...', profile_image.image
profile_image.save()
except Exception as err:
print 'Error: ', err.message
useraccount = request.session.get('useraccount', False)
return render(request, 'profileImage.html', {'useraccount': useraccount, 'image': profile_image})
出现错误时,我刚尝试了很多方面,所以非常感谢您的帮助。
答案 0 :(得分:0)
request.FILES
是一个文件列表。您需要指定要传递给image
参数的文件。喜欢这个。
profile_image = ProfileImage(account_id=request.session.get('memberid', False), image=request.FILES[0], profile='', description='')
这不是很好,因为如果没有文件,它会引发异常。您可能需要使用Django form来更好地处理表单。