关于临时图像测试和在models.py中压缩图像的测试,我遇到了一个奇怪的问题。权限似乎有问题:
ERROR: test_has_light_images (realestate.tests.test_view_listing.RealestateListingViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\test\utils.py", line 373, in inner
return func(*args, **kwargs)
File "C:\Users\Storm\Dev\btre_project\realestate\tests\test_view_listing.py", line 72, in test_has_light_images
create_listing(title='listing_sample', address='sample', realtor_num=1, city='sample', state='sample', zipcode='1234', price='555555', bedrooms='1', bathrooms='1', garage='1', sqft='123', lot_size='123', image_sample=image_sample.name)
File "C:\Users\Storm\Dev\btre_project\realestate\tests\test_view_listing.py", line 37, in create_listing
return Listing.objects.create(title=title, address=address, realtor=realtor, city=city, state=state, zipcode=zipcode, price=price, bedrooms=bedrooms, bathrooms=bathrooms, garage=garage, sqft=sqft, lot_size=lot_size, photo_main=image_sample, photo_1=image_sample, photo_2=image_sample, photo_3=image_sample, photo_4=image_sample, photo_5=image_sample, photo_6=image_sample)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\Storm\Dev\btre_project\realestate\models.py", line 69, in save
new_image = compress(self.photo_main)
File "C:\Users\Storm\Dev\btre_project\realestate\models.py", line 11, in compress
im = Image.open(image)
File "C:\Users\Storm\Envs\btre\lib\site-packages\PIL\Image.py", line 2774, in open
fp.seek(0)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\utils.py", line 20, in <lambda>
seek = property(lambda self: self.file.seek)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\fields\files.py", line 43, in _get_file
self._file = self.storage.open(self.name, 'rb')
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\storage.py", line 36, in open
return self._open(name, mode)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\storage.py", line 224, in _open
return File(open(self.path(name), mode))
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Storm\\AppData\\Local\\Temp\\tmp24xoaa7g'
test.py
from django.test import TestCase
from django.urls import reverse, resolve
from django.utils import timezone
import datetime
from ..models import Listing
from django.test import override_settings
from PIL import Image
from io import BytesIO
import tempfile
def get_temporary_image(temp_file):
size = (200, 200)
color = (255, 0, 0, 0)
image = Image.new("RGB", size, color)
image.save(temp_file, 'jpeg')
return temp_file
@override_settings(MEDIA_ROOT=tempfile.gettempdir())
def test_has_light_images(self):
temp_file = tempfile.NamedTemporaryFile()
image_sample = get_temporary_image(temp_file)
Listing.objects.create(title='listing_sample', photo_main=image_sample.name)
models.py
def compress(image):
im = Image.open(image)
im_io = BytesIO()
im.save(im_io, 'JPEG', quality=20, optimize=True)
new_image = File(im_io, name=image.name)
return new_image
class Listing(models.Model):
title = models.CharField(max_length=200)
photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
def save(self, *args, **kwargs):
new_image = compress(self.photo_main)
self.photo_main = new_image
super().save(*args, **kwargs)
我尝试了另一种压缩图像的方法,该方法可以使测试正常进行,但是在压缩图像方面没有成功。
有人知道这里发生了什么吗?
答案 0 :(得分:0)
这在使用Pycharm的Windows计算机上发生。
解决方法:Right-click
您的PyCharm应用程序并以管理员身份运行。
更新: “从Windows的开始菜单中,右键单击Pycharm快捷方式,然后选择“以管理员身份运行”。
或
更改要保存目录的权限,以便所有用户都具有读写权限。