我是Django框架的新手。我正在尝试将图像上载到customer表,出现以下错误:
_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
。错误的原因是什么?如何解决
设置.py
STATIC_URL = '/static/'
MEDIA = '/images/'
STATICFILES_DIRS = [BASE_DIR, 'static']
MEDIA_ROOT = [BASE_DIR, 'static/images']
型号.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中
请参阅此https://docs.djangoproject.com/en/3.1/howto/static-files/
我也有同样的问题,这可能有效 将其更改为:
对于未定义的错误
os
,您只需导入它相关问题 更多 >
编程相关推荐