在 django 中上传图像时出错:“强制转换为 Unicode:需要字符串或缓冲区,找到元组”
尝试在 django 中使用 ImageField。 这是我的模型
class Album(models.Model):
title = models.CharField(max_length=100)
def __unicode__(self):
return self.title
class Photo(models.Model):
image = models.ImageField(upload_to='photos/')
album = models.ForeignKey(Album)
title = models.CharField(max_length=100, default="")
def __unicode__(self):
return self.title
class PhotoModelForm(forms.ModelForm):
class Meta:
model = Photo
这是urls.py的一部分
...
url(r'^trial/upload/$', 'trial.views.upload'),
...
views.py
def upload(request):
if request.method == 'POST':
form = PhotoModelForm(request.POST, request.FILES)
if form.is_valid():
photo = form.save()
return render_to_response('trial/thanks_upload.html',{
'photo': photo
}, context_instance = RequestContext(request))
else:
form = PhotoModelForm()
return render_to_response('trial/upload.html', {
'form': form
}, context_instance = RequestContext(request))
upload.html
<form enctype="multipart/form-data" action="/trial/upload/" method="post">
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Upload" /></p>
</form>
但是在保存时我有下一个错误: /试用/上传/处出现类型错误 强制转换为 Unicode:需要字符串或缓冲区,找到元组
photo.save 上出现错误
有人知道为什么会这样吗?为什么会出现元组?我确信有一个愚蠢的错误......
Trying to work with ImageField in django.
Here are my models
class Album(models.Model):
title = models.CharField(max_length=100)
def __unicode__(self):
return self.title
class Photo(models.Model):
image = models.ImageField(upload_to='photos/')
album = models.ForeignKey(Album)
title = models.CharField(max_length=100, default="")
def __unicode__(self):
return self.title
class PhotoModelForm(forms.ModelForm):
class Meta:
model = Photo
Here is a part of urls.py
...
url(r'^trial/upload/
views.py
def upload(request):
if request.method == 'POST':
form = PhotoModelForm(request.POST, request.FILES)
if form.is_valid():
photo = form.save()
return render_to_response('trial/thanks_upload.html',{
'photo': photo
}, context_instance = RequestContext(request))
else:
form = PhotoModelForm()
return render_to_response('trial/upload.html', {
'form': form
}, context_instance = RequestContext(request))
upload.html
<form enctype="multipart/form-data" action="/trial/upload/" method="post">
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Upload" /></p>
</form>
But on saving I have next error:
TypeError at /trial/upload/
coercing to Unicode: need string or buffer, tuple found
Errors appears on photo.save
Does anybody has ideas why could it be? Why tuple appears at all? I'm sure there is a stupid bug...
, 'trial.views.upload'),
...
views.py
upload.html
But on saving I have next error:
TypeError at /trial/upload/
coercing to Unicode: need string or buffer, tuple found
Errors appears on photo.save
Does anybody has ideas why could it be? Why tuple appears at all? I'm sure there is a stupid bug...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己已经拿到了。在settings.py中有MEDIA_ROOT设置,这是
Python因为末尾的逗号而创建对象元组。这就是它无法保存该对象的原因。下次注意你的逗号!
I've got it myself. In settings.py there is MEDIA_ROOT setting, which was
Python makes the object tuple because of the comma at the end. That's why it couldn't save the object. Watch your commas next time!