【问题标题】:TypeError: _getfullpathname: path should be string, bytes or os.PathLike, not list (Django)TypeError:_getfullpathname:路径应该是字符串、字节或 os.PathLike,而不是列表(Django)
【发布时间】:2021-10-31 01:46:07
【问题描述】:

我正在尝试通过管理面板中的“客户”模型向客户添加个人资料照片。

models.py

from django.contrib.auth.models import User

class Customer(models.Model):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    profile_pic = models.ImageField(null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    
    def __str__(self):
        return self.name

settings.py

STATIC_URL = '/static/'

MEDIA_URL = '/imagenes/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
]
 
MEDIA_ROOT = [BASE_DIR/'static/images']

我认为我在设置静态文件路径时出错了;事实是我对配置它的方式知之甚少,我不明白为什么会发生错误..请有人可以帮助我

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    settings.MEDIA_ROOT 应该是路径而不是列表,您需要更改设置。让你的静态目录和媒体目录重叠也不是一个好主意,你应该为你的媒体使用一个唯一的目录

    MEDIA_ROOT = BASE_DIR / 'media'
    

    https://docs.djangoproject.com/en/3.2/ref/settings/#media-root

    【讨论】:

    • 你能解释一下如何为媒体配置一个唯一目录吗?@IainShelvington
    • @Lewcamps 像我的回答一样设置媒体根目录,它会将上传的文件放在一个名为“媒体”的目录中,与您的静态文件分开
    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多