Search code examples
javascripthtmldjangodjango-viewsdjango-forms

I am getting multivalue key error in views in django


I created a Review System When I am going to submit the review there is an error In my views.py:

def ajex_add_review(request,pid):
    product=Product.objects.get(pk=pid)
    user=request.user

    review=ProductReview.objects.create(
        user=user,
        product=product,
        review=request.POST['review'],#1
        rating=request.POST['rating'],#2
        )
    context={
        'user':user.username,
        'review':request.POST['review'],#1
        'rating':request.POST['rating'],#2
    }
    average_reviews=ProductReview.objects.filter(product=product).aggregate(rating=Avg("rating"))

    return JsonResponse(
      {
        'bool':True,
        'context':context,
        'average_reviews':average_reviews,
      }
    )

I think that the error coming from here. Because if I comment out review(#1) or rating(#2) any of this it is working perfectly well

In case if you need my js file then In function.js:

console.log("working");


$("#commentForm").submit(function(e){
    e.preventDefault();

    $.ajex({
        data: $(this).serialize(),

        method: $(this).attr("method"),

        url: $(this).attr("action"),

        dataType: "json",

        success: function(response){
            console.log("Comment Saved");
        }
    })
})

and In my product-detail.html

                  <div class="add-review">
                      <h3>Add Your Review</h3>
                      <form action="{%  url 'core:ajex_add_review' p.id  %}" method="POST" id="commentForm">
                        {% csrf_token %}
                          {{review_form.rating}}
                      </form>
                      <br>
                      <form action="{%  url 'core:ajex_add_review' p.id  %}" method="POST" id="commentForm">
                        {% csrf_token %}
                          {{review_form.review}}
                          <button type="submit" class="btn">Submit</button>
                      </form>

                      <div class="rating-bar">
                        <div class="bar">
                          <div class="fill" style="width: 80%;"></div>
                        </div>
                        <div class="bar-label"><i>{{average_rating.rating|floatformat:1}} out of 5.0</i></div>
                      </div>

                  </div>

So please help me out from this problem. And my Indentation are correct. And at last My error is:

System check identified 2 issues (0 silenced).
March 08, 2024 - 01:45:45
Django version 4.1.13, using settings 'ecomprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /ajex-add-review/3/
Traceback (most recent call last):
  File "C:\Users\Sagarmoy Sadhukhan\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\datastructures.py", line 84, in __getitem__
    list_ = super().__getitem__(key)
            ^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'rating'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Sagarmoy Sadhukhan\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 56, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sagarmoy Sadhukhan\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sagarmoy Sadhukhan\Desktop\django ecom web\ecomprj\core\views.py", line 114, in ajex_add_review
    rating=request.POST['rating'],
           ~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\Sagarmoy Sadhukhan\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\datastructures.py", line 86, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'rating'
[08/Mar/2024 01:45:46] "POST /ajex-add-review/3/ HTTP/1.1" 500 75801

Solution

  • The problem is that you have two forms in your template! That's why your request.POST contains the key 'rating' two times.

    One review_form contains a rating and a review. So one form should be sufficient.

    <div class="add-review">
        <h3>Add Your Review</h3>
        <form action="{%  url 'core:ajex_add_review' p.id  %}" method="POST" id="commentForm">
        {% csrf_token %}
            {{review_form.rating}}
    <--- remove stuff here ---->
            {{review_form.review}}
            <button type="submit" class="btn">Submit</button>
        </form>
    
        <div class="rating-bar">
        <div class="bar">
            <div class="fill" style="width: 80%;"></div>
        </div>
        <div class="bar-label"><i>{{average_rating.rating|floatformat:1}} out of 5.0</i></div>
        </div>
    
    </div>
    

    As an additional info I want to recommend using not request.POST but using this syntax

    form = ReviewForm(request.POST)
    if form.is_valid():
        review=ProductReview.objects.create(
            user=user,
            product=product,
            review=form.cleaned_data['review'],#1
            rating=form.cleaned_data['rating'],#2
            )
    else:
        # some error
    
    # rest of your code
    

    This allows you to easily incorporate the form validation logic.