0

I have a model which contains user_image field. This field has default image value:

user_image = models.ImageField(verbose_name='Аватар',blank=True, default=settings.STATIC_URL+'avatar.jpeg')

after update to django 1.10 this default image doesn't show in templates because link contain wrong path. For example in my case link should be

'127.0.0.0:8000/static/avatar.jpeg'

instead I have link like this

'127.0.0.0:8000/media/static/avatar.jpeg'

Python console outputs

>>>u.userprofiletable_set.get().user_image
<ImageFieldFile: /static/avatar.jpeg>

>>> u.userprofiletable_set.get().user_image.url
'/media/static/avatar.jpeg'

>>> u.userprofiletable_set.get().user_image.path
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/korablevop/.virtualenvs/mynewenv/lib/python3.5/site-packages/django/db/models/fields/files.py", line 64, in _get_path
    return self.storage.path(self.name)
  File "/Users/korablevop/.virtualenvs/mynewenv/lib/python3.5/site-packages/django/core/files/storage.py", line 407, in path
    return safe_join(self.location, name)
  File "/Users/korablevop/.virtualenvs/mynewenv/lib/python3.5/site-packages/django/utils/_os.py", line 78, in safe_join
    'component ({})'.format(final_path, base_path))
django.core.exceptions.SuspiciousFileOperation: The joined path (/static/avatar.jpeg) is located outside of the base path component (/Users/korablevop/PycharmProjects/sxodu/media)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Oleg
  • 777
  • 1
  • 6
  • 10
  • please, show your settings.py file – Zagorodniy Olexiy Nov 20 '16 at 18:20
  • `STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), 'static', ) MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') MEDIA_URL = '/media/'` – Oleg Nov 20 '16 at 18:23
  • I tried to emulate your situation, but in my case everything work fine. I find this post with similar problem as you http://stackoverflow.com/questions/33625173/suspicious-file-operation-the-joined-path-is-located-outside-of-the-base-pa, hope it help you. – Zagorodniy Olexiy Nov 20 '16 at 20:26

1 Answers1

0

In the definition of user_image, you have the following:

default=settings.STATIC_URL+'avatar.jpeg'

This does not make any sense. First, STATIC_URL is a URL, not a filesystem path, and here you are using it as a filesystem path. Second, don't confuse static and media files. user_image is media, not static.

Django created the link 127.0.0.0:8000/media/static/avatar.jpeg, because, according to the above, your user_image is /static/avatar.jpeg. It concatenated this with MEDIA_URL, so it became /media//static/avatar.jpg. Maybe it normalized the URL by removing the double slash, but the thing is, you have it totally wrong.

You might find my post, How Django static files work in production, helpful.

Antonis Christofides
  • 6,990
  • 2
  • 39
  • 57
  • It worked on Django 1.9 and I can't understand in what difference between django versions – Oleg Nov 22 '16 at 07:52
  • I don't believe there is much difference between the two Django versions in this respect. It may have been something else that broke it. The thing is, it doesn't matter, because what you were doing was working by chance. – Antonis Christofides Nov 22 '16 at 08:03