因此,我一直在尝试调整上传到我的个人网站的项目页面上的图像的大小,但由于某种原因,它使我出错。
我正在尝试调整PIL的大小,但是每当我发布带有上传图像的帖子时,都会不断抛出属性错误。
我的代码在下面:
from django.db import models
from django.utils import timezone
from django.forms import ModelForm
from django.utils.text import slugify
from django.utils.crypto import get_random_string
from PIL import Image
# Create your models here.
class Projects(models.Model):
title = models.CharField(max_length=30)
description = models.TextField(max_length=150)
publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
update_date = models.DateTimeField(auto_now=True, auto_now_add=False)
slug = models.SlugField(unique=True)
files = models.FileField(upload_to='files/',blank=True)
images = models.ImageField(upload_to='images/',blank=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
# Opening the image to be resized
r_image = Image.open(self.images)
# Resizing the image from the imagefield
new_image = r_image.resize((96, 96), Image.ANTIALIAS)
self.images = new_image
# Generates a random string
unique_string = get_random_string(length=32)
# Combines title and unique string to slugify
slugtext = self.title + "-" + "unique_id=-" + unique_string
self.slug = slugify(slugtext)
return super(Projects, self).save(*args, **kwargs)
感谢您的回答!
哦!这是我的完整回溯
Internal Server Error: /create/
Traceback (most recent call last):
File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/erichardson/env01/personalwebsite/projects/views.py", line 21, in create_projects
project_form.save()
File "/home/erichardson/env01/lib/python3.6/site-packages/django/forms/models.py", line 456, in save
self.instance.save()
File "/home/erichardson/env01/personalwebsite/projects/models.py", line 39, in save
return super(Projects, self).save(*args, **kwargs)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1282, in execute_sql
for sql, params in self.as_sql():
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1235, in as_sql
for obj in self.query.objs
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1235, in <listcomp>
for obj in self.query.objs
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1234, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1184, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/fields/files.py", line 285, in pre_save
if file and not file._committed:
AttributeError: 'Image' object has no attribute '_committed'