上传文件时无法通过 'tuple' object has no attribute 'startswith' 错误
Uploading a file and I can't get past the 'tuple' object has no attribute 'startswith' error
我遇到了这个错误
AttributeError at /admin/user/usermodel/1/
'tuple' object has no attribute 'startswith'
当我尝试向管理员上传文件时,我对文件上传真的很陌生,无法理解
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/home/user/project/foo/yesno/static/'
STATICFILES_DIRS = (
'/home/user/project/foo/yesno/static_store',
)
MEDIA_ROOT = '/home/user/project/foo/yesno/static/media',
MEDIA_URL = '/media/'
models.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
forms.py
class ThumbnailForm(forms.Form):
thumbnail = forms.FileField()
views.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})
行
MEDIA_ROOT = '/home/user/project/foo/yesno/static/media',
将 MEDIA_ROOT
定义为一个元组。具体来说,由于尾随逗号,它使它成为一个单项元组:元组由逗号定义,而不是通常认为的由括号定义(空元组除外:-)。
删除可能是意外的尾随逗号,这个特定错误应该会消失。
我遇到了这个错误
AttributeError at /admin/user/usermodel/1/
'tuple' object has no attribute 'startswith'
当我尝试向管理员上传文件时,我对文件上传真的很陌生,无法理解
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/home/user/project/foo/yesno/static/'
STATICFILES_DIRS = (
'/home/user/project/foo/yesno/static_store',
)
MEDIA_ROOT = '/home/user/project/foo/yesno/static/media',
MEDIA_URL = '/media/'
models.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
forms.py
class ThumbnailForm(forms.Form):
thumbnail = forms.FileField()
views.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})
行
MEDIA_ROOT = '/home/user/project/foo/yesno/static/media',
将 MEDIA_ROOT
定义为一个元组。具体来说,由于尾随逗号,它使它成为一个单项元组:元组由逗号定义,而不是通常认为的由括号定义(空元组除外:-)。
删除可能是意外的尾随逗号,这个特定错误应该会消失。