0

i have a simple form for adding comments and in views i am checking it for validity. But it always returns False and i get error404 according to my code. I have several assumption, but it didn`t work. And now i have no idea how to get the reason views.py:

class ProductDetailView(DetailView):
    model = Product
    template_name = "core/product.html"
    context_object_name = "product"
    ....

    def post(self, request, *args, **kwargs):
        if request.POST.get("product_detail_form") == 'add_comment_form_two':
            if self.request.user.is_authenticated:
                form = CommentForm(request.POST)
                if form.is_valid():
                    comment = Review(review=request.POST.get("review"),
                                     rating=request.POST.get("rating"),
                                     author=self.request.user,
                                     product_connected=self.get_object())

                    comment.save()
                    print("success ")
                    return redirect("product_detail", *args, **kwargs)
                else:
                    print("error")
                    raise Http404    
                    ....

html form

<form action="" method="post">
    {% csrf_token %}
    {{ comment_form.as_p }}
    <input type="submit" name="product_detail_form" 
           value="add_comment_form_two">add comment</input>
</form>

forms.py

class CommentForm(forms.ModelForm):
    review = forms.CharField(
        required=True,
        widget=forms.TextInput(
            attrs={"class": "comment_form", "placeholder": "Comment"}), label=""
    )
    OPTIONS = (
        ("1", "1"),
        ("2", "2"),
        ("3", "3"),
        ("4", "4"),
        ("5", "5"),
    )
    rating = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                       choices=OPTIONS)
    class Meta:
        model = Review
        fields = ["review", "rating"]

model

class Review(models.Model):
    review = models.TextField(max_length=500)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    product_connected = models.ForeignKey(Product, on_delete=models.CASCADE, 
                                         related_name="reviews")
    creation_date = models.DateTimeField(auto_now_add=True)
    last_update_time = models.DateTimeField(auto_now=True)
    rating = models.IntegerField(default=0, validators=[MinValueValidator(0), 
                                 MaxValueValidator(5)]) 

Thanks for any help, I just can't understand it for a long time.

stupid rule that I have to write less code than question because my question is two sentences

2
  • 1
    You should do something with the form.errors instead of printing a random string and raising an error
    – Sayse
    May 20, 2022 at 20:45
  • ok, i`ll try it May 20, 2022 at 21:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.