django ModelForm 不捕获上传的图像
django ModelForm not capturing uploaded images
您好,我正在尝试为我网站上的用户创建一个表单,以便能够添加产品,我使用 ModelForm 制作了该表单,并且我已经设法在我的模板中呈现它,但它无法正常工作必需的。在提交表单时,我不断收到验证错误,显示图像尚未提交但我确实添加了它们,任何想法
型号
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=120)
price = models.FloatField()
image_182x182 = models.ImageField(upload_to='pdt_imgs/')
image_1200x1200 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
image_600x600 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
image_600x600_2 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
image_300x300 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
img_array = [image_1200x1200, image_600x600, image_600x600_2]
sku = models.IntegerField()
available = models.BooleanField(default=True)
discount = models.IntegerField(default = 0)
category = models.ForeignKey(SubCategory, on_delete=models.CASCADE)
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('pdt_detail', args=[str(self.id)])
def get_add_to_cart_url(self):
return reverse('add-to-cart', args= [str(self.id)] )
表格
from .models import Product
from django.forms import ModelForm
class ProductForm(ModelForm):
class Meta:
model = Product
fields = [
'name', 'price', 'image_182x182', 'image_1200x1200', 'image_600x600',
'image_600x600_2', 'image_300x300', 'sku', 'available', 'discount',
'category', 'seller'
]
观看次数
from django.utils.decorators import method_decorator
from .decorators import seller_required
from django.views.generic import CreateView
from store.models import Product
from store.forms import ProductForm
from django.contrib import messages
@method_decorator( seller_required , name='dispatch')
class SellerProductAddView(CreateView):
model = Product
form_class = ProductForm
template_name = 'seller_add_pdt.html'
def form_valid(self, form):
product = form.save()
messages.success(self.request, "Your Product was succesfully added")
return redirect('seller_add_pdt')
模板
<div class="ps-block__content">
<form class="ps-form--account-setting" method="post">
{% csrf_token %}
{{ form|crispy }}
<div class="form-group submit">
<button type="submit" class="ps-btn">Add</button>
</div>
</form>
</div>
<form class="ps-form--account-setting" method="post" enctype="multipart/form-data">
您好,我正在尝试为我网站上的用户创建一个表单,以便能够添加产品,我使用 ModelForm 制作了该表单,并且我已经设法在我的模板中呈现它,但它无法正常工作必需的。在提交表单时,我不断收到验证错误,显示图像尚未提交但我确实添加了它们,任何想法
型号
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=120)
price = models.FloatField()
image_182x182 = models.ImageField(upload_to='pdt_imgs/')
image_1200x1200 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
image_600x600 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
image_600x600_2 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
image_300x300 = models.ImageField(upload_to='pdt_imgs/alt_imgs/')
img_array = [image_1200x1200, image_600x600, image_600x600_2]
sku = models.IntegerField()
available = models.BooleanField(default=True)
discount = models.IntegerField(default = 0)
category = models.ForeignKey(SubCategory, on_delete=models.CASCADE)
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('pdt_detail', args=[str(self.id)])
def get_add_to_cart_url(self):
return reverse('add-to-cart', args= [str(self.id)] )
表格
from .models import Product
from django.forms import ModelForm
class ProductForm(ModelForm):
class Meta:
model = Product
fields = [
'name', 'price', 'image_182x182', 'image_1200x1200', 'image_600x600',
'image_600x600_2', 'image_300x300', 'sku', 'available', 'discount',
'category', 'seller'
]
观看次数
from django.utils.decorators import method_decorator
from .decorators import seller_required
from django.views.generic import CreateView
from store.models import Product
from store.forms import ProductForm
from django.contrib import messages
@method_decorator( seller_required , name='dispatch')
class SellerProductAddView(CreateView):
model = Product
form_class = ProductForm
template_name = 'seller_add_pdt.html'
def form_valid(self, form):
product = form.save()
messages.success(self.request, "Your Product was succesfully added")
return redirect('seller_add_pdt')
模板
<div class="ps-block__content">
<form class="ps-form--account-setting" method="post">
{% csrf_token %}
{{ form|crispy }}
<div class="form-group submit">
<button type="submit" class="ps-btn">Add</button>
</div>
</form>
</div>
<form class="ps-form--account-setting" method="post" enctype="multipart/form-data">