0

I am learning django forms right now. I can not see the output of the error in the website as a text message, it only shows as a pop-up message with the default error message even though I set my own error message. Also, my console is not printing anything after

if form.is_valid():

This is my views.py file:

def forms(request):
    if request.method == 'POST':
        form = Forms(request.POST)

        if form.is_valid():
            print("hello")
            form.save()
            print(form.cleaned_data)
            return HttpResponseRedirect('/thank_you')
    else: 
        form = Forms()
    

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

def thank_you(request):
    return render(request, 'form/thankyou.html')

And here's the html file

<html lang='en'>
<head>
    <meta charset="UTF-8">
</head>
<body>
  <form action="/thank-you" method="POST">
    {% csrf_token %}
    {{ form.name.label_tag}} <br>
    {{form.name}} <br>
    {{form.name.errors}}
    <button type="submit">Send</button> 

  </form>
1
  • "it only shows as a pop-up message", I think you talk about the browser doing some validation there. Try adding the novalidate attribute to your form tag: <form action="/thank-you" method="POST" novalidate> does that do what you expect? Commented Oct 5, 2021 at 10:57

1 Answer 1

0

Try this in your template to show error message:

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <strong>{{ error|escape }}</strong>
        {% endfor %}
    {% endfor %}
{% endif %}

And Here is solution for custom error message.

Sign up to request clarification or add additional context in comments.

Comments

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.