Django:MEDIA_ROOT error : _getfullpathname: path should be string, bytes or os.PathLike, not tuple
Django:MEDIA_ROOT error : _getfullpathname: path should be string, bytes or os.PathLike, not tuple
我是 Django 框架的新手。我正在尝试将图像上传给客户 table,但出现此错误:
_getfullpathname: path should be string, bytes or os.PathLike
我正在使用此代码上传:
MEDIA_ROOT = [BASE_DIR, 'static/images']
我把代码改成了
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
但它说 NameError: name 'os' not defined
。报错的原因是什么,如何解决?
settings.py
STATIC_URL = '/static/'
MEDIA = '/images/'
STATICFILES_DIRS = [BASE_DIR, 'static']
MEDIA_ROOT = [BASE_DIR, 'static/images']
models.py
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=11, null=True)
email = models.EmailField(max_length=200, null=True)
profile_pic = models.ImageField(null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
试试这个
在settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
MEDIA = '/images/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = BASE_DIR + 'static/images'
参考这个https://docs.djangoproject.com/en/3.1/howto/static-files/
我有同样的问题,这可能有效
将其更改为:
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
对于错误 os
not defined 你只需要导入它
import os
from pathlib import Path
我是 Django 框架的新手。我正在尝试将图像上传给客户 table,但出现此错误:
_getfullpathname: path should be string, bytes or os.PathLike
我正在使用此代码上传:
MEDIA_ROOT = [BASE_DIR, 'static/images']
我把代码改成了
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
但它说 NameError: name 'os' not defined
。报错的原因是什么,如何解决?
settings.py
STATIC_URL = '/static/'
MEDIA = '/images/'
STATICFILES_DIRS = [BASE_DIR, 'static']
MEDIA_ROOT = [BASE_DIR, 'static/images']
models.py
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=11, null=True)
email = models.EmailField(max_length=200, null=True)
profile_pic = models.ImageField(null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
试试这个
在settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
MEDIA = '/images/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = BASE_DIR + 'static/images'
参考这个https://docs.djangoproject.com/en/3.1/howto/static-files/
我有同样的问题,这可能有效 将其更改为:
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
对于错误 os
not defined 你只需要导入它
import os
from pathlib import Path