Newbie question...where's the request body gone?


Have been following the standard tutorial and have hit a wee issue. The detail.html template:

    <form action="{% url 'polls:vote' %}" method="post">

        {% csrf_token %}

            <legend><h1>{{ question.question_text }}</h1></legend>

            {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

            {% for choice in question.choice_set.all %}
            <input type="radio" name="choice{{ forloop.counter }}" value="{{ }}">
            <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
            {% endfor %}
        <!-- The "vote" in the submit value tells Django to call the "vote" function-->
        <input type="submit" value="vote">

when processed by the vote function:

def vote(request, question_id):

    questionObj = get_object_or_404(Question, pk=question_id)

        if request.method == 'POST':
            if 'choice' in request.POST:
                selectedChoiceObj = questionObj.choice_set.get(pk=request.POST['choice'])

                # body_unicode = request.body.decode('utf-8')
                # body_data = json.loads(body_unicode)
                    dict = request.POST[0]
                    return render(request, 'polls/detail.html', {'question': questionObj, 'error_message': "No POST[0] "})

                    body_data = dict.keys()
                    return render(request, 'polls/detail.html', {'question': questionObj, 'error_message': "No keys() method or an"})

                return render(request, 'polls/detail.html', {'question': questionObj, 'error_message': "No choice in the request object: " + body_data})
            return render(request, 'polls/detail.html', {'question': questionObj, 'error_message': "Request is not a POST"})

    except (KeyError, Choice.DoesNotExist):
        #Redisplay the question voting form with the error
        return render(request, 'polls/detail.html', {'question': questionObj, 'error_message': "You didn't select a choice"})
        selectedChoiceObj.votes += 1
        #Redirect to the results page having voted
        return HttpResponseRedirect(reverse('polls:results', args=(

Is failing at the try wrapping my attempt to dict = request.POST[0]. Thus indicating that there is no querydictionary object being returned in the body. The lines above trying to deserialise the body also fail with a message that states the body value is None.

I’m using Python 3.8.5, django 3.2.3, Pycharm CE IDE (and no debugging available that I can find in it).

Any ideas?

(Side note: dict is a built-in method, you really don’t want to use it as a variable name)

request.POST is a dict - the keys are the names of the form fields, so the keys would be something like “choice1”, “choice2”, etc, not 0.

Also, POST data is not JSON. It’s key=value pairs separated by newlines. (You can see the data being sent using your browser’s developer tools on the “network” tab.)

Thanks. I have been able to manipulate the dict object and work it out a bit better.

I’ve got other issues now, but that’s another post.