在 django 中上传图像时出错:“强制转换为 Unicode:需要字符串或缓冲区,找到元组”

发布于 2024-11-11 16:20:19 字数 1830 浏览 0 评论 0原文

尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-11-18 16:20:19

我自己已经拿到了。在settings.py中有MEDIA_ROOT设置,这是

MEDIA_ROOT = 'd:/dev/python/scripts/app/media/',

Python因为末尾的逗号而创建对象元组。这就是它无法保存该对象的原因。下次注意你的逗号!

I've got it myself. In settings.py there is MEDIA_ROOT setting, which was

MEDIA_ROOT = 'd:/dev/python/scripts/app/media/',

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文