我有一个更新的视图,我需要更改图像字段
models.py 我需要编辑的表格:
class Item(models.Model):
# custom validators
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed.')
# fields
dress_name = models.ForeignKey(Name, on_delete=models.DO_NOTHING, blank=False, verbose_name='نوع الفستان',)
dress_rate = models.ForeignKey(Rate, on_delete=models.DO_NOTHING, blank=False, verbose_name='تصنيف الفستان',)
dress_size = models.ForeignKey(Size, on_delete=models.DO_NOTHING, verbose_name='مقاس الفستان', blank=False)
dress_color = models.CharField(max_length=50, verbose_name='لون الفستان', blank=False)
dress_description = models.TextField(max_length=800, verbose_name=' وصف إضافى للفستان', blank=False)
dress_image1 = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, blank=False, verbose_name='الصورة الأساسية للفستان',
help_text='لا يمكنك تركها فارغة')
dress_image2 = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, blank=False, verbose_name='صورة إضافية ')
dress_image3 = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, blank=False, verbose_name='صورة إضافة ')
dress_image4 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, blank=True, verbose_name='صورة إضافة ')
dress_image5 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, blank=True, verbose_name='صورة إضافة ')
dress_action = models.ForeignKey(Action, on_delete=models.DO_NOTHING, verbose_name='الفستان معروض لل ',
help_text='للبيع او للإيجار ', blank=False)
dress_price = models.IntegerField(default=1, verbose_name='السعر', blank=False)
dress_mobile = models.CharField(max_length=20, validators=[alphanumeric], verbose_name='رقم الهاتف ', blank=False)
created_by = models.CharField(max_length=250,)
created_username = models.CharField(max_length=250, default='unknown')
created_at = models.DateTimeField(auto_now=True)
dress_active = models.BooleanField(default=False)
dress_special = models.BooleanField(default=False)
dress_town = models.ForeignKey(Town, on_delete=models.DO_NOTHING, verbose_name='المحافظة', blank=False)
urls.py ,其中包含更新视图的网址:
path('dress/<int:pk>/update/', views.dress_update.as_view(), name="dress_update"),
views.py 视图本身:
class dress_update(UpdateView):
model = Item
fields = ['dress_name', 'dress_rate', 'dress_size', 'dress_color','dress_description', 'dress_image1', 'dress_image2',
'dress_image3', 'dress_image4', 'dress_image5', 'dress_action', 'dress_price', 'dress_mobile', 'dress_town']
template_name_suffix = '_update_form'
def form_valid(self, form):
form.instance.dress_active = False
return super(dress_update, self).form_valid(form)
def get_success_url(self):
view_name = 'dress_update'
# No need for reverse_lazy here, because it's called inside the method
return reverse('dress_update_confirm')
HTML 表单:
<form method="post" >{% csrf_token %}
{{ form.as_p }}
<input class="btn btn-info" type="submit" value="تعديل" />
</form>
一切都很好但是当用户点击上传图片并尝试将新图片上传到图片域时..没有错误但图片没有更新