1

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, num2 and result)
  • in your view, populate num1 and num2 with the numbers to be added
  • render the form in your template (num1 and num2 fields should be read only)
  • the user enters the answer in the result field 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?

2 Answers 2

1

Do something like below: if form is valid then check for the condition otherwise post blank form; if form is valid but result answer is wrong then redirect to previous url you desire to redirect

def form_handle(request):
    if request.method == 'POST':
        form = MyForm(request.POST) # if post method then form will be validated
        if form.is_valid():
            cd = form.cleaned_data
            num1 = cd.get('num1')
            num2 = cd.get('num2')
            result = cd.get('result')
            if float(num1) + float(num2) == float(result):
                # give HttpResponse only or render page you need to load on success
                return HttpResponse("valid entiries")
            else:
                # if sum not equal... then redirect to custom url/page 
                return HttpResponseRedirect('/')  # mention redirect url in argument

    else:
        form = MyForm() # blank form object just to pass context if not post method
    return render(request, "rr.html", {'form': form})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for fixing my error, I see the issue now. !
0

If I understand correctly, the form that you call in the html file is the form in the function post_question in views.py, isn't it? And where is the class QuestionForm?, can you show us the code of it? Because as I see, the class MyForm is still unused in file views.py. It would be easier if you also show us the import in views.py and the class QuestionForm instead of just a nonsensical MyForm. Cheer!

1 Comment

Hi I fixed my post up, I accidentally added the wrong views function. For the import statement, I did from .views import *, and from .forms import *

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.