Hi,
Have been following the standard tutorial and have hit a wee issue. The detail.html template:
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset>
<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="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
<br>
{% endfor %}
</fieldset>
<!-- The "vote" in the submit value tells Django to call the "vote" function-->
<input type="submit" value="vote">
</form>
when processed by the vote function:
def vote(request, question_id):
questionObj = get_object_or_404(Question, pk=question_id)
try:
if request.method == 'POST':
if 'choice' in request.POST:
selectedChoiceObj = questionObj.choice_set.get(pk=request.POST['choice'])
else:
# body_unicode = request.body.decode('utf-8')
# body_data = json.loads(body_unicode)
try:
dict = request.POST[0]
except:
return render(request, 'polls/detail.html', {'question': questionObj, 'error_message': "No POST[0] "})
try:
body_data = dict.keys()
except:
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})
else:
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"})
else:
selectedChoiceObj.votes += 1
selectedChoiceObj.save()
#Redirect to the results page having voted
return HttpResponseRedirect(reverse('polls:results', args=(questionObj.id)))
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?