У меня есть форма для добавления объектов к моделям, без ошибок для всего процесса, но и без добавления каких-либо объектов в модели. Я что-то пропустил?
ниже моя модель, поле соответствует формам, но не всем.
product_name = models.CharField(max_length=200)
price = models.DecimalField(decimal_places=2, max_digits=10, blank=True)
created = models.DateTimeField(auto_now=True)
img = models.ImageField(upload_to='product', default='xxxxx')
img2 =models.ImageField(upload_to='product', null=True, default='xxxxxx')
img3 =models.ImageField(upload_to='product', null=True, default='xxxxx')
img4 =models.ImageField(upload_to='product', null=True, default='xxxxxxx')
storage_amount = models.PositiveIntegerField(validators=[MinValueValidator(0)])
out_of_storage_or_not = models.BooleanField(default=False)
description = models.TextField(blank=True, null=True)
Hot = models.BooleanField(default=False)
type = models.CharField(
max_length=2,
choices=PRODUCT_CHOICES,
default=xxxxx,
)
status = models.IntegerField(choices=STATUS, default=0)
slug = models.SlugField(max_length=255, unique=True)
forms.py:
class Add_Product(forms.ModelForm):
product_name = forms.CharField()
price = forms.DecimalField()
img = forms.ImageField()
img2 = forms.ImageField()
img3 = forms.ImageField()
storage_amount = forms.IntegerField()
description = forms.CharField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 20}))
Hot = forms.BooleanField(widget=forms.CheckboxInput())
type = forms.ChoiceField(widget=forms.RadioSelect, choices=PRODUCT_CHOICES)
status = forms.ChoiceField(choices=STATUS)
slug = forms.CharField()
class Meta:
model = Product
fields = ('product_name', 'price', 'img', 'img2', 'img3', 'storage_amount', 'description', 'Hot', 'type', 'status', 'slug')
views.py:
if request.method == 'POST':
form = Add_Product(data=request.POST)
if form.is_valid():
product = form.save(commit=False)
product.save()
messages.success(request, 'Success')
return redirect('/')
else:
form = Add_Product()
context = {'form': form}
return render(request, 'add_product.html', context)
add_product. html:
<form method="POST" class="card-body" >
{% csrf_token %}
<h3 style="text-align: center">Add Product</h3>
{{ form | crispy }}
<button type="submit">Submit</button>
</form>