This is a continuation from last question here Coding mental block with specific Django task
The answer was:
A pure django solution would be:
- create a form with three integer fields (say,
num1,num2andresult) - in your view, populate
num1andnum2with the numbers to be added - render the form in your template (
num1andnum2fields should be read only) - the user enters the answer in the
resultfield and submits the form - in your view, determine whether
num1 + num2 == result - redirect to a success page if the answer is correct, otherwise redisplay the form
However, as I am working through the steps, my form was not being rendered properly.
views.py
def form_handle(request):
form = MyForm()
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
a = cd.get('a')
return render(request, "rr.html", {})
forms.py
class MyForm(forms.Form):
a = forms.CharField(max_length=20)
mat = forms.CharField(max_length=200)
html file
<form action="{% url 'form_handle' %}" method="POST">{% csrf_token %}
{{form.as_p}}
<button type="submit">Submit</button>
</form>
When I load the page all I see is a submit button. As pictured
Can someone please advise me where I had gone wrong?