在我的个人资料页面上,注册时会自动为用户分配默认图像。但是在更改了静态和媒体文件夹的工作方式之后,它给了我以下错误:
django.core.exceptions.SuspiciousFileOperation: The joined path (/static/public/images/default.jpg) is located outside of the base path component (/mnt/c/Users/maxlo/Documents/apps/adinl/adinl/media).
我认为它正在尝试在媒体中查找图像,但是此后它已移至static / public / images / default.jpg
我尝试过the following,但似乎没有用。
如果有人对如何解决这个问题有个好主意。
型号:
from django.db import models
from django.contrib.auth.models import User
from django.core.files import File
from PIL import Image
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='/static/public/images/default.jpg', upload_to='profile_pictures', verbose_name='Profile Picture')
def __str__(self):
return f'{self.user.username} Profile'
def save(self, force_insert=False, force_update=False, using=None):
super().save()
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)
Settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
文件结构: