简体   繁体   English

文件上传-django管理员-OSError-没有此类文件或目录

[英]file upload - django admin - OSError - no such file or directory

I am trying to save image to imagefield of a model thru admin page but i am getting this error message: 我正在尝试通过管理页面将图像保存到模型的imagefield中,但出现此错误消息:

[Errno 2] No such file or directory

this is my model 这是我的模特

def custom_path_tc(instance, filename):
    return '/'.join(['topiccenter',instance.title,filename])

class TopicCenter(models.Model):
    title = models.TextField()
    image = models.ImageField(upload_to=custom_path_tc,default='',blank=True,null=True)

and this is my admin.py 这是我的admin.py

from django.contrib import admin
from models import TopicCenter

admin.site.register(Person)

settings.py: settings.py:

MEDIA_ROOT = os.path.join(PROJECT_PATH, "media")
MEDIA_URL = "/media/"

under media folder the new folder will be created during upload but on the way i am getting this error message, what am i doing wrong here? media文件夹下,新文件夹将在上传过程中创建,但是在我收到此错误消息的途中,我在这里做错了什么?

As you are using path 'topiccenter / instance.title / filename' to store the file, you need to create 'tocpiccenter/instance.title ' directory if it does not exists. 当您使用路径'topiccenter / instance.title / filename'存储文件时,如果目录'tocpiccenter/instance.title ”不存在,则需要创建'tocpiccenter/instance.title目录。

Do that in custom_path_tc() function. custom_path_tc()函数中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM