Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django上传文件时会删除;“违约”;形象_Django_Python 3.x_Django Models - Fatal编程技术网

Django上传文件时会删除;“违约”;形象

Django上传文件时会删除;“违约”;形象,django,python-3.x,django-models,Django,Python 3.x,Django Models,我有一个Ourteam应用程序,可以让你上传员工的图片、姓名、头衔和社交媒体信息。每当我创建一个对象时,“default.jpg”文件就会从媒体根目录中删除 这是我的模型: from django.db import models from cms.models.pluginmodel import CMSPlugin from django.utils.translation import ugettext_lazy as _ from smartfields import fields fr

我有一个Ourteam应用程序,可以让你上传员工的图片、姓名、头衔和社交媒体信息。每当我创建一个对象时,“default.jpg”文件就会从媒体根目录中删除

这是我的模型:

from django.db import models
from cms.models.pluginmodel import CMSPlugin
from django.utils.translation import ugettext_lazy as _
from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor
from django.template.defaultfilters import slugify


class Employee(CMSPlugin):

    # Set Name
    name = models.CharField(_('name'), max_length=48)

    # Define Slug
    slug = models.SlugField(max_length=40, null = False, blank = True)

    # Set Title
    title = models.CharField(_('title'), max_length=48)

    # Set Image upload path and image properties
    image_upload_path = 'ourteam/%Y/%m/%d'

    image = fields.ImageField(upload_to=image_upload_path, 
        blank=True, default='ourteam/default.jpg', 
        dependencies=[
            FileDependency(processor=ImageProcessor(
                format='JPEG', scale={'max_width': 150, 'max_height': 150}))
        ])

    created = models.DateTimeField(_('created'), auto_now_add=True)
    email = models.EmailField(_('email'), max_length=254)

    # Social Media
    twitter = models.CharField(_('twitter'), max_length=24, blank=True, default='https://www.twitter.com')
    linkedin = models.CharField(_('linkedin'), max_length=24,blank=True, default='https://www.linkedin.com')
    facebook = models.CharField(_('facebook'), max_length=24,blank=True, default='https://www.facebook.com')

    class Meta:
        verbose_name = _('employee')
        verbose_name_plural = _('employee')
        db_table  = 'employee'
        ordering  = ('-created',)
        get_latest_by = 'created'

    def __unicode__(self):
        return u'%s' % self.title

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Employee, self).save(*args, **kwargs)

    def get_all_employees():
        all_entries = Employee.objects.all().order_by('created')
        return all_entries

    def slug(sluggy):
        sluggy = sluggy.replace(' ', '-').lower()
        return slugify(sluggy)

你应该试试这个:

def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.user.id, filename)

class MyModel(models.Model):
upload = models.FileField(upload_to=user_directory_path)
def用户目录路径(实例,文件名):
#文件将上载到媒体\u ROOT/用户_/
返回'user{0}/{1}'。格式(instance.user.id,文件名)
类MyModel(models.Model):
upload=models.FileField(upload\u to=user\u directory\u path)

尝试更改给定
default.jpg的权限,使应用程序无法删除它(
chown root
chmod 000
,无论什么),这样您将看到一个可帮助您识别问题的回溯。