2017-04-20 69 views
1

我想上傳個人資料圖片。圖像沒有上傳。它給在/配置文件中的錯誤AttributeError的/'list'對象沒有屬性'startswith'

'名單' 對象有沒有屬性 'startswith'

def userDetail(request): 
current_user = request.user 
if(request.method == 'POST'): 
    form = UserDetailDataForm(request.POST, request.FILES) 
    if(form.is_valid()): 
     profileImg = request.FILES['profileImg'] 
     try: 
      userprofile = UserDetail.objects.get(user_id = current_user.id) 
      if userprofile: 
       userprofile.profileImg = profileImg 
       userprofile.save() 

     context = {'username': current_user.username, 'image' : userprofile.profileImg,            'MEDIA_ROOT': settings.MEDIA_ROOT} 
     return render_to_response('user_profile.html', context) 
else: 
    form = UserDetailDataForm() 
return render_to_response('profile.html', {'form': form}) 

Models.py

profileImg = models.ImageField(upload_to='/images/',blank=True, null=True,default="images/default-profile.jpg")

設置文件:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
MEDIA_ROOT = [ 
os.path.join(BASE_DIR, 'firstapp/static/'),] 

Full StackTrace:

Environment: 


Request Method: POST 
Request URL: http://localhost:8000/profile/ 

Django Version: 1.10.5 
Python Version: 2.7.12 
Installed Applications: 
['django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'firstapp', 
'events', 
'django_extensions'] 
Installed Middleware: 
['django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware'] 



Traceback: 

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner 
    39.    response = get_response(request) 

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view 
    58.   return view_func(*args, **kwargs) 

File "/var/www/html/myproject/firstapp/views.py" in userDetail 
    161.     userprofile.save() 

File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/base_user.py" in save 
    80.   super(AbstractBaseUser, self).save(*args, **kwargs) 

File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save 
    796.      force_update=force_update, update_fields=update_fields) 

File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base 
    824.    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) 

File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table 
    886.      for f in non_pks] 

File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in pre_save 
    292.    file.save(file.name, file, save=False) 

File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in save 
    91.   self.name = self.storage.save(name, content, max_length=self.field.max_length) 

File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in save 
    53.   name = self.get_available_name(name, max_length=max_length) 

File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in get_available_name 
    77.   while self.exists(name) or (max_length and len(name) > max_length): 

File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in exists 
    394.   return os.path.exists(self.path(name)) 

File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in path 
    407.   return safe_join(self.location, name) 

File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__ 
    35.   res = instance.__dict__[self.name] = self.func(instance) 

File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in location 
    283.   return abspathu(self.base_location) 

File "/usr/local/lib/python2.7/dist-packages/django/utils/_os.py" in abspathu 
    30.   if not isabs(path): 

File "/usr/lib/python2.7/posixpath.py" in isabs 
    54.  return s.startswith('/') 

Exception Type: AttributeError at /profile/ 
Exception Value: 'list' object has no attribute 'startswith' 
+1

Full stacktrace? –

+0

我已編輯帖子。 –

回答

4

MEDIA_ROOT setting應該是,而不是一個列表:

MEDIA_ROOT = os.path.join(BASE_DIR, 'firstapp/static/') 
+0

我在MEDIA_ROOT多個路徑 MEDIA_ROOT = [ os.path.join(BASE_DIR, 'firstapp /靜態/'), os.path.join(BASE_DIR, '事件/靜態/'), ] –

+0

@ ManpreetAhluwalia的權利,請遵循常見的方式從django項目應用程序收集靜態文件(@ Tryph的答案是關於它)。謝謝。 – alecxe

0

如果您有多個應用程序的靜態文件,你應該看看staticfiles app doc,讓您收集多個應用程序的靜態文件在一個可以指向您的STATIC_ROOT設置的獨特位置。

相關問題