我有一個模型,其中包含user_image字段。該字段的默認圖像值:Django 1.10不顯示圖像
user_image = models.ImageField(verbose_name='Аватар',blank=True, default=settings.STATIC_URL+'avatar.jpeg')
後更新的Django 1.10此默認圖像模板沒有顯示,因爲鏈接包含錯誤的道路。例如,在我的情況下鏈接應該
'127.0.0.0:8000/static/avatar.jpeg'
,而不是我有這樣
'127.0.0.0:8000/media/static/avatar.jpeg'
Python的控制檯輸出
>>>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)
請出示settings.py文件 –
'STATIC_URL = '/靜態/' STATICFILES_DIRS =( os.path.join(BASE_DIR, '靜態'), '靜態', ) MEDIA_ROOT = OS .path.join(BASE_DIR,'media /') MEDIA_URL ='/ media /'' – Oleg
我試圖模仿你的情況,但在我的情況下一切工作正常。我發現這個帖子有類似的問題,因爲你http://stackoverflow.com/questions/33625173/suspicious-file-operation-the-joined-path-is-located-outside-of-the-base-pa,希望它幫助您。 –