I want to upload the profile picture. the image is not getting uploaded. It gives the error AttributeError at /profile/
'list' object has no attribute '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")
Settings file:
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'
MEDIA_ROOT
setting should be a string, not a list:
MEDIA_ROOT = os.path.join(BASE_DIR, 'firstapp/static/')