此后端不支持绝对路径

This backend doesn't support absolute paths

我有

This backend doesn't support absolute paths

尝试登录时。下面是我的代码

models.py

from django.db import models
from django.contrib.auth.models import User
from PIL import Image

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    role = models.CharField(default='user', max_length=20)

def __str__(self):
    return f'{self.user.username} Profile'

def save(self, *args, **kwargs):
    super().save(*args, **kwargs)
    img = Image.open(self.image.path)
    if img.width > 280 or img.height > 280:
        output_size = (280, 280)
        img.thumbnail(output_size)
        img.save(self.image.path)

settings.py

STATICFILES_DIRS = [
BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR / "staticfiles-cdn" # in production, we want cdn

MEDIA_ROOT = BASE_DIR / "staticfiles-cdn" / "media"

from .cdn.conf import * # noqa

在 django-project 文件夹>cdn 我有

backends.py

from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage

class StaticRootS3Boto3Storage(S3Boto3Storage):
   location = 'static'

class MediaRootS3Boto3Storage(S3Boto3Storage):
   location = 'media'

conf.py

import os

AWS_ACCESS_KEY_ID=os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY=os.environ.get("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME=os.environ.get("AWS_STORAGE_BUCKET_NAME")
AWS_S3_ENDPOINT_URL="my digitalocean endpoint"


AWS_S3_OBJECT_PARAMETERS = {
   "CacheControl": "max-age=86400"
}
AWS_LOCATION = "https://{AWS_STORAGE_BUCKET_NAME}.fra1.digitaloceanspaces.com"

DEFAULT_FILE_STORAGE = "appname.cdn.backends.MediaRootS3Boto3Storage"

STATICFILES_STORAGE = "appname.cdn.backends.StaticRootS3Boto3Storage" 

好的,这终于对我有用了。

在我的 forms.py

from django.template import Context

def save(self):from django.template import Context
    user = super().save()
    x = self.cleaned_data.get('x')
    y = self.cleaned_data.get('y')
    w = self.cleaned_data.get('width')
    h = self.cleaned_data.get('height')

    image = Image.open(self.image)
    cropped_image = image.crop((x, y, w + x, h + y))
    resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)

    fh = storage.open(self.image.name, "w")
    picture_format = 'png'
    resized_image.save(fh, picture_format)
    fh.close()
    resized_image.save(self.image.path)
    return user