Django |创建新的配置文件对象时出现SuspiciousFileOperation错误

时间:2018-12-27 09:18:59

标签: django

创建新的个人资料时出现错误:

SuspiciousFileOperation at /create_profil
The joined path C:\Users\user\PycharmProjects\project\media\photo\2018\12\27\jhg.png) is 
located outside of the base path component 
(C:\Users\user\PycharmProjects\project\media\)

更新:此代码在我同事计算机上的Ubuntu OS上正常工作。当我从git存储库中提取工作时,它对我不起作用。 这些是我的设置:

STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
print("---------------------------" + MEDIA_ROOT)
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")

这是我的模特

class Profil(models.Model):
    nom = models.CharField(max_length=120)
    image = models.ImageField(default='defaut.png', upload_to='image/%Y/%m/%d', blank=False, null=True)

如何摆脱这个错误?

更新:这是我的BASE_DIR

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

3 个答案:

答案 0 :(得分:0)

更改此STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')

答案 1 :(得分:0)

您也可以尝试以下方法:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/' 

将此添加到您的urls.py

if settings.DEBUG:
    '''Here MEDIA_URL and MEDIA_ROOT is added to the urlpatterns.
       So that this will allow our media to work within the browser.
    '''
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

答案 2 :(得分:0)

我已经有几天了。我发现将join中的第二个参数从“ media /”更改为“ media”似乎可行,尽管我真的无法告诉您为什么会这样。