上载文件时我无法通过“tuple”对象没有属性“startswith”

2024-10-01 15:35:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了这个错误

AttributeError at /admin/user/usermodel/1/
'tuple' object has no attribute 'startswith'

当我试着把一个文件上传到管理员,我真的是一个新的文件上传,不能弄明白

在设置.py在

^{pr2}$

在模型.py在

def get_upload_file_name(instance, filename):
    return "uploaded_files/%s_%s" % (str(time()).replace('.','_'), filename)

filename = ''

# Create your models here.

class UserModel(models.Model):
    user        = models.OneToOneField(User)
    position    = models.IntegerField(default=1)
    thumbnail   = models.FileField(upload_to="uploaded_files", blank=True)

    def __unicode__(self):
        return self.user.username

在表单.py在

class ThumbnailForm(forms.Form):

    thumbnail = forms.FileField()

在视图.py在

def handle_uploaded_file(f):
    with open('some/file/name.txt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

def UploadPicRequest(request):
    if request.method == 'POST':
        thumb_form = ThumbnailForm(request.POST, request.FILES)
        if thumb_form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/')
        else:
            thumb_form = ThumbnailForm()
        return render_to_response('/update_profile/error.html', {'thumb_form': thumb_form})

Tags: 文件namepyformreturnmodelsrequestdef
1条回答
网友
1楼 · 发布于 2024-10-01 15:35:11

线

MEDIA_ROOT = '/home/user/project/foo/yesno/static/media',

MEDIA_ROOT定义为元组。元组通常被逗号定义为空,除非后面的元组是空的元组:-). 在

删除可能是意外的尾随逗号,这个特定的错误应该会消失。在

相关问题 更多 >

    热门问题