Sunday, June 30, 2019

Django Model form not saving items to database

Model
class Products(models.Model):

UNIT = (
('whole', 'whole unit'),
('item', 'per single item'),
)


# category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
ProductName = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE)
ProductDescription = models.TextField(max_length=500, blank=True)
price = models.FloatField()
location = models.CharField(choices = COUNTIES, max_length=300)
# category = models.CharField( choices = CATEGORIES, max_length=10, default='other')
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)

unitofsale = models.CharField(max_length=10, choices=UNIT)
image = models.FileField(upload_to='products_images/', blank=True)
sublocation = models.CharField(max_length=100)
created= models.DateTimeField(auto_now_add=True)
# slug = models.SlugField(max_length=200,db_index=True)

class Meta:
ordering = ('-created',)
# index_together = (('id', 'slug'),)

template
        <form method="POST" action="{% url  'sell' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="row col-md-10">
<div class="col-md-6">
<div class="form-group">
<label>Product Name</label>
{{ form.ProductName}}

</div>


</div>
<div class="col-md-6">
<div class="form-group">
<label>Product Price</label>
{{ form.price }}
</div>
</div>
</div>
<div class="row col-md-10">
<div class="col-md-6">
<div class="form-group">
<label>County</label>
{{ form.location}}

</div>


</div>
<div class="col-md-6">
<div class="form-group">
<label>Sub-location</label>
{{ form.sublocation }}
</div>
</div>
</div>
<div class="row col-md-10">
<div class="col-md-6">
<div class="form-group">
<label>Category</label>
{{ form.category}}

</div>


</div>
<div class="col-md-6">
<div class="form-group">
<label>Unit of Sale</label>
{{ form.unitofsale }}
</div>
</div>
</div>
<div class="row col-md-10">
<div class="col-md-6">
<div class="form-group">
<label>Product Description</label>
{{ form.ProductDescription}}

</div>


</div>
<div class="col-md-6">
<div class="form-group">
<label>Upload Product Image</label>
{{ form.image }}
</div>
</div>
</div>
<div class="row col-md-10">
<div class="col-md-6">
<div class="form-group">
<button type="submit" class="btn btn-success btn-lg mt-5"> Sell </button>

</div>


</div>
</div>


<br>

{# <input type="submit" value="Sell">#}
</form>
view 
def sell(request):
if request.method == 'POST':
form = ProductsForm()
form = ProductsForm(request.POST, request.FILES, instance = request.user)
if form.is_valid():
print('form is valid')
form = form.save(commit=True)
user = request.user
form.user = user
form.save()
return redirect('home')

else:
form = ProductsForm()
return render(request, 'sell/products_form.html', {'form': form})

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4b2be89e-f505-4ad5-80eb-b6c2965d35a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment