当我尝试使用图像字段更新表单时,此错误不断出现。我正在尝试上传或更新客户 LOGO 图像。
当我尝试上传图片时,包含图片的字段显示“[field] 属性没有与之关联的文件”
我正在使用模型表单和通用更新视图。我不知道如何使属性具有与之关联的文件。
我在模型中尝试了 save() 覆盖,但它仍然给了我这个错误。
模型.py
class Customers(models.Model):
associations = models.ManyToManyField(Association)
company_name = models.CharField(help_text='Company Name', max_length=200, default=None)
contact1_first_name = models.CharField(help_text='Primary Contact First name', max_length=50, default=None)
contact1_last_name = models.CharField(help_text='Primary Contact Last name', max_length=50, default=None)
address1 = models.CharField(help_text='Address 1', max_length=50, default=None)
address2 = models.CharField(help_text='Address 2', max_length=50, default=None, blank=True)
city = models.CharField(help_text='City', max_length=50, default=None)
state = models.CharField(help_text='State', max_length=50, default=None)
zip_code = models.CharField(help_text='Postal/ZIP', max_length=20, default=None)
phone = models.CharField(help_text='Phone', max_length=20, default=None)
email = models.EmailField(default=None)
contact2_first_name = models.CharField(max_length=50, null=True, blank=True)
contact2_last_name = models.CharField(max_length=50, null=True, blank=True)
contact2_phone = models.CharField(help_text='Phone', max_length=20, default=None, null=True, blank=True)
contact2_email = models.EmailField(default=None, null=True, blank=True)
fax = models.CharField(max_length=20, null=True, blank=True)
mobile = models.CharField(max_length=50, default=None, blank=True, null=True)
web_address = models.CharField(max_length=50, null=True, blank=True)
date_entered = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
date_turned_off = models.DateTimeField(default=None, null=True, blank=True)
num_locs = models.SmallIntegerField(default=1)
notes = models.TextField(help_text='Notes', null=True, blank=True, default=None)
vertical_cat = models.IntegerField(default=0)
custom_cat = models.IntegerField(default=0)
logo = models.FileField(upload_to='media/cust-logs/', blank=True)
max_scripts = models.IntegerField(default=0)
branch_custom_scripts = models.BooleanField(default=False)
receive_emails = models.BooleanField(default=True)
customer_rep_id = models.SmallIntegerField(default=0, blank=True, null=True)
customer_writer_id = models.SmallIntegerField(default=0, blank=True, null=True)
customer_male_vt_id = models.SmallIntegerField(default=0, blank=True, null=True)
customer_fem_vt = models.SmallIntegerField(default=0, blank=True, null=True)
customer_sales_rep = models.SmallIntegerField(default=0, blank=True, null=True)
contract_date_first = models.DateTimeField(default=None, null=True, blank=True)
contract_date_renewal = models.DateTimeField(default=None, null=True, blank=True)
contract_billing_type = models.CharField(max_length=200, null=True, blank=True)
anniversary = models.DateTimeField(default=None, null=True, blank=True)
is_on_contract = models.BooleanField(default=False)
internal_flag = models.IntegerField(default=0, blank=True, null=True)
shuff_freq_pref = models.SmallIntegerField(default=0, null=True, blank=True)
shuff_freq_upd = models.DateTimeField(default=None, null=True, blank=True)
shuff_freq_7 = models.BooleanField(default=True)
shuff_freq_14 = models.BooleanField(default=True)
shuff_freq_30 = models.BooleanField(default=True)
shuff_freq_60 = models.BooleanField(default=True)
shuff_freq_90 = models.BooleanField(default=True)
shuff_freq_180 = models.BooleanField(default=False)
shuff_walk_thru = models.DateTimeField(default=None, null=True, blank=True)
shuff_walk_thru_by = models.ForeignKey(Emp, related_name='+', on_delete=models.CASCADE)
cust_classification = models.ForeignKey(CustomerClasses, related_name='+', on_delete=models.CASCADE)
date_updated = models.DateTimeField(auto_now=True, null=True, blank=True)
updated_by = models.SmallIntegerField(default=0)
audit_note = models.TextField(default=None, null=True, blank=True)
audit_exception = models.BooleanField(default=False)
audit_date = models.DateTimeField(auto_now_add=True)
script_exception = models.BooleanField(default=False)
script_exception_date = models.DateTimeField(default=None, null=True, blank=True)
reports_exception = models.BooleanField(default=False)
reports_exception_date = models.DateTimeField(default=None, null=True, blank=True)
using_script_credits = models.BooleanField(default=False)
def __str__(self):
return self.company_name
def save(self):
super().save()
img = Image.open(self.logo.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.logo.image)
我将其更改为上传到 S3,记录中有一个图像路径,并显示“此后端不支持绝对路径”。
视图.py
class CustomerUpdateView(LoginRequiredMixin, UpdateView):
model = Customers
template_name = 'customers/customer_form.html'
fields = 'company_name', 'contact1_first_name', 'contact1_last_name', 'contact2_first_name', \
'contact2_last_name', 'contact2_email', 'contact2_phone', 'address1', 'address2', \
'city', 'state', 'zip_code', 'phone', 'email', 'mobile', 'web_address', 'notes', \
'cust_classification', 'logo'
success_url = 'customers:cust-home'
def form_valid(self, form):
return super().form_valid(form)
def save(self):
user = super(CustomerForm, self).save()
x = self.cleaned_data.get('x')
y = self.cleaned_data.get('y')
w = self.cleaned_data.get('w')
h = self.cleaned_data.get('h')
image = Image.open(user.primaryphoto)
cropped_image = image.crop(x, y, w + x, h, + y)
resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
fh = storage.open(user.primaryphoto.name, "wb")
picture_format = 'png'
resized_image.save(fh, picture_format)
fh.close()
return user
我从另一个 StackOverflow 帖子中借用了上面的一些 save() 部分。
这是我的客户表格
表格.py
class CustomerForm(forms.ModelForm):
class Meta:
model = Customers
fields = 'company_name', 'contact1_first_name', \
'contact1_last_name', 'email', 'contact2_first_name', \
'contact2_last_name', 'contact2_email', \
'contact2_phone', 'address1', 'address2', \
'city', 'state', 'zip_code', 'phone', \
'mobile', 'web_address', 'notes', \
'cust_classification', 'shuff_walk_thru_by', 'logo', 'associations'
今天我的 Django 项目一团糟。它很大,我已经为此工作了 2 个月,哈哈。