简体   繁体   English

Django DEBUG=False 不会上传文件到 STATIC_ROOT

[英]Django DEBUG=False won't upload files to STATIC_ROOT

I'm trying to give the user the ability to upload images on a website I created with Django and the images are uploaded fine when running with DEBIG=True but the issue is that when DEBUG=False the image files doesn't upload to the MEDIA_ROOT instead it gets uploaded to STATIC_DIRS .我试图让用户能够在我使用Django创建的网站上上传图像,并且在使用DEBIG=True运行时图像上传正常,但问题是当DEBUG=False时图像文件不会上传到MEDIA_ROOT而不是它被上传到STATIC_DIRS Also, even the files that are already in MEDIA_ROOT after executing python manage.py collectstatic aren't loaded to the template and the image URL gives the 404 Page Not Found error.此外,即使在执行python manage.py collectstatic后已经在MEDIA_ROOT中的文件也不会加载到模板中,图像 URL 会出现404 Page Not Found错误。

The CSS and JS files are still served so it means only the media url isn't working. CSS 和 JS 文件仍在提供,因此这意味着只有媒体 url 不工作。

Following are the codes I'm using.以下是我正在使用的代码。

urls.py网址.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Home.urls')),
    path('account/', include('Account.urls')),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py设置.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

Note that I'm using whitenoise as the storage backend.请注意,我使用whitenoise作为存储后端。

Here is the model code that is used to upload the file.这是用于上传文件的 model 代码。

@deconstructible
class PathAndRename(object):
    def __init__(self, sub_path):
        self.path = sub_path

    def __call__(self, instance, filename):
        ext = filename.split('.')[-1]
        filename = '{}.{}'.format(uuid4().hex, ext)
        return os.path.join(self.path, filename)

rename_pp = PathAndRename('img/profile-pictures')

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    avatar = models.ImageField(upload_to=rename_pp, blank=True, null=True, default='img/profile-pictures/default-profile-pic.png')

    def save(self, *args, **kwargs):
        super(Profile, self).save(*args, **kwargs)
        image = Image.open(self.avatar.path)
        if image.width > 100 or image.height > 100:
            output_size = (100, 100)
            image.thumbnail(output_size, Image.ANTIALIAS)
            image.save(self.avatar.path, file_quality=100)

When using DEBUG=True it means that you are at development side thus your local machine is able to serve the files (media files) but with DEBUG=False it is taken like you are now on deployment side.使用DEBUG=True时,这意味着您处于开发端,因此您的本地计算机能够提供文件(媒体文件),但使用DEBUG=False时,它就像您现在处于部署端一样。

To serve media files from that, you need to have a bucket(like amazon S3) which will store those files and will get served from there.要从中提供媒体文件,您需要有一个存储桶(如 amazon S3)来存储这些文件并从那里获得服务。

So if your media files are working fine when DEBUG=True , that means it is fine.因此,如果您的媒体文件在DEBUG=True时工作正常,那就意味着它很好。

Now all you need is a place to store them.现在您只需要一个存放它们的地方。

I've used this instead of your STATICFILES_STORAGE and when debug=False everything works fine.我用这个代替你的 STATICFILES_STORAGE 并且当 debug=False 一切正常。

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

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

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