MEDIA_ROOT错误:_getfullpathname:路径应为字符串,字节或os.PathLike,而不是元组 [英] MEDIA_ROOT error : _getfullpathname: path should be string, bytes or os.PathLike, not tuple

查看:59
本文介绍了MEDIA_ROOT错误:_getfullpathname:路径应为字符串,字节或os.PathLike,而不是元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django框架的新手.当我在设置中使用 MEDIA_ROOT = os.path.join(BASE_DIR,'media')命令时,尝试在

I'm new to Django's framework. When I use the MEDIA_ROOT = os.path.join(BASE_DIR, 'media'), command in the settings, I encounter the following error when i trying to upload an image in http://127.0.0.1:8000/admin/products/product/add/ (admin mode):

_getfullpathname:路径应为字符串,字节或os.PathLike,而不是元组

当我尝试删除MEDIA_ROOT = ...时,也会消除该错误,并且图像将正确放置在媒体文件夹的路径上.我认为使用MEDIA_ROOT的原因是为了了解媒体文件到Django的路径,但是:

as i try i found deleting MEDIA_ROOT=... will also erase the error and the images will be placed correctly on the path on media's folder. I think the reason for using MEDIA_ROOT is to understand the path of the media files to Django, but :

1)为什么在使用它时会遇到错误

1)why when I use it I will encounter an error

2)为什么我删除此命令,一切进展顺利?谢谢

2) why I delete this command, everything goes smoothly? Thanks

setting.py

setting.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media'),
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static'),

错误屏幕截图为 getfullpathname:路径应为字符串,字节或os.PathLike,而不是元组

project/urls.py

project/urls.py

from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
#url will be here
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我的模型中只有一个图像场.py:

i have just an imagefield in my models.py:

from django.db import models

# Create your models here.


class Product(models.Model) :
    name = models.CharField(max_length=100 , verbose_name="نام جنس",null=True,blank=True)
    category = models.ForeignKey('Category',on_delete=models.CASCADE , verbose_name="دسته بندی" , null=True ,blank=True)
    price = models.IntegerField(verbose_name="قیمت" ,null=True,blank=True)
    property=models.ForeignKey('Property',on_delete=models.CASCADE , verbose_name="ویژگی" , null=True,blank=True)
    description = models.TextField(verbose_name="توضیحات",null=True,blank=True)
    image=models.ImageField(upload_to="media/productimage/")


class Property(models.Model):
    color = models.CharField(max_length=40 , verbose_name="رنگ",null=True,blank=True)
    made = models.CharField(max_length=40 , verbose_name="ساخت کشور" , null=True,blank=True) #made in country


class Category(models.Model):
    name = models.CharField(max_length=100 , verbose_name="دسته بندی" , null=True,blank=True)

推荐答案

settings.py 中的 MEDIA_ROOT STATIC_ROOT 变量的值末尾有逗号.尾部逗号将这些变量的值从字符串转换为元组.

The value of both the MEDIA_ROOT and STATIC_ROOT variables in your settings.py have a trailing comma. The trailing comma turns the value of these variables from a string into a tuple.

MEDIA_ROOT = os.path.join(BASE_DIR, 'media'),  # This is a tuple
STATIC_ROOT = os.path.join(BASE_DIR, 'static'),  # So is this

删除尾部逗号应该可以解决问题.

Removing the trailing comma should resolve the problem.

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')  # This is now a string
STATIC_ROOT = os.path.join(BASE_DIR, 'static')  # So is this

这篇关于MEDIA_ROOT错误:_getfullpathname:路径应为字符串,字节或os.PathLike,而不是元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆