Select не создает запись категории в базе данных django
Мне нужно сделать так, чтобы селект из формы отображался в базе данных, None появлялся на своем месте в базе данных
добавляется все, кроме категорий и акций Мне сказали, что нужно добавить стоимость к опциону, но у меня ничего не произошло
что случилось
Вот мой views.py :
def create_product(request):
categories = Category.objects.all()
stock = Stock.objects.all()
if request.method == 'POST':
product = Product()
product.title = request.POST.get('title')
product.category = Category.objects.get(id=request.POST.get('category'))
product.price = request.POST.get('price')
product.user = request.POST.get('user')
product.amount = request.POST.get('amount')
product.stock = request.POST.get('stocks')
product.user = request.user
product.save()
return redirect('index')
return render(request, 'main/create.html', {'categories':categories,'stocks':stock})
Вот мой models.py :
class Product(models.Model):
title = models.CharField(verbose_name="Название товара", max_length=250)
categories = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
price = models.IntegerField(verbose_name="Цена")
user = models.ForeignKey(get_user_model(),on_delete=models.CASCADE,related_name='products')
amount = models.CharField(max_length=250,verbose_name='Количество')
user = models.ForeignKey(User, on_delete=models.PROTECT)
stock = models.ForeignKey(Stock, on_delete=models.PROTECT, blank=True, null=True)
def get_absolute_url(self):
return reverse("post_detail",kwargs={'pk':self.pk})
class Meta:
verbose_name = 'Товар'
verbose_name_plural = 'Товары'
def __str__(self):
return self.title
Мой шаблон создания :
<div class="" style="width: 600px; margin:0 auto; transform: translate(0, 10%);">
<h1 class="display-6">Создайте товар</h1>
<form action="{% url 'create_product' %}" method="POST" class="form-control" style="background:rgba(0, 0, 0, 0.1); height: 500px;">
{% csrf_token %}
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Название</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Название товара" name="title">
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Цена</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Цена товара" name="price">
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Количество товара</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Кол-во" name="amount">
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Категория</label>
<select class="form-select" aria-label="Default select example" name="category">
{% for categories in categories %}
<option value="">{{ categories.title }}</option>
{% endfor %}
</select>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Склад</label>
<select class="form-select" aria-label="Default select example" name="stock">
{% for stocks in stocks %}
<option value="{{ stocks.pk }}" >{{ stocks.title }}</option>
{% endfor %}
</select>
</div>
<div class="mb-3">
<button class="btn btn-success">Создать</button>
<a href="{% url 'analytics' %}" class="btn btn-secondary">Выйти</a>
</div>
</form>
</div>