Posted 12 September 2022, 4:07 am EST
I’m trying to send JS data to Views ( index of a an item). When a customer clicks on one of the stars to rate the product, JS gets the index of the star +1, and some how I would like to add this const listIndex = newChildren.indexOf(item) + 1 to the product review form. On Views, set review.rating = listIndex. Is there a way to do it? Any help will be greatly appreciated. Thank you. Here is the code snippets:
@login_required
def product_review(request, pk):
user = request.user
customer = Customer.objects.filter(name=user).first()
product = Product.objects.filter(pk=pk).first()
if request.method == “POST”:
form = ProductReviewForm(request.POST)
if form.is_valid():
review_form = form.save()
review = ProductReview.objects.get(pk=review_form.pk)
review.author= customer
# review.rating = “”
review.product.add(product)
review.save()
return redirect(“store:my_orders”)
else:
form = ProductReviewForm()
context = {
“form”: form,
“product”: product
}
return render(request, “store/product_review.html”, context)
class ProductReview(models.Model):
product = models.ManyToManyField(Product, related_name=“product_review”)
author = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
review_title = models.CharField(max_length=100)
content = models.TextField()
date = models.DateTimeField(default=timezone.now)
rating = models.IntegerField(default=0)
def __str__(self): return f"{self.author}: {self.review_title}"