Cannot get is_valid() to go back to template with bound form

In the Django docs, it says

“We call the form’s is_valid() method; if it’s not True, we go back to
the template with the form. This time the form is no longer empty
(unbound) so the HTML form will be populated with the data previously
submitted, where it can be edited and corrected as required.”

I am trying to get this behaviour to work.

In views.py:

def test(request):
    if request.method == 'POST':
        form = TestForm(request.POST)

        if form.is_valid():
            return redirect('auctions/test.html')

    else:
        form = TestForm()
        print(form)
        return render(request, 'auctions/test.html', {'form': form})

In forms.py:

class TestForm(forms.Form):
    testnumber = forms.DecimalField(max_digits=1)

In test.html:

<form action="{% url 'auctions:test' %}" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>

Now, when I submit the form with a number that is more than 1 digit, is_valid() evaluates to False. When that happens, the docs says the template should be rendered with the bound form and the error. Instead, nothing happens, and I get ValueError: The view auctions.views.test didn't return an HttpResponse object. It returned None instead.

Go to Source
Author: Ryan Eom

several forms in 1 html file and closing a modal when data is valid

i am new to django and html along with css. created my html and css and now working on developing with django. I have 3 forms in 1 html file. login, register and forgot password. Have been breaking my head and couldn’t find a proper solution. what i did so far is create the register form (which is a modal done with css) but don’t know how to define a new form in the same html.
views file:

def index(request):
form = RegisterForm()

if request.method == 'POST':
    form = RegisterForm(request.POST)
    if form.is_valid():
        form.save()
        user = form.cleaned_data.get('username')
        messages.success(request, 'Membership was created for ' + user)
        return redirect('http://127.0.0.1:8000')

context = {'form':form}
return render(request, "index.html", context)

how to add another form which is under index.html?
also i tried the above method for the redirect to close the modal but it is not closing
appreciate someone’s help to explain to a newbie on the solution or provide some link with a solution. on how to add another form to the index.hml and hoe to close the modal once the form is valid and submitted

Go to Source
Author: rabih