Home > Mobile >  jinja2.exceptions.UndefinedError: 'forms.ContactForm object' has no attribute 'messag
jinja2.exceptions.UndefinedError: 'forms.ContactForm object' has no attribute 'messag

Time:06-15

I am trying to create a contact form using flask, but keep getting this error when I run the app in debug mode and open the render webpage.

It's my browser error when I'm open my site in local host:

UndefinedError
jinja2.exceptions.UndefinedError: 'forms.ContactForm object' has no attribute 'message'

my files :

first.py

from flask import Flask, flash, make_response, redirect, render_template, request, session, url_for, abort
from werkzeug.utils import secure_filename
from flask_mail import Mail, Message
from forms import ContactForm

app = Flask(__name__)

app.secret_key = "example"

# Contact form
@app.route('/contact', methods=['POST', 'GET'])
def contact():
    form = ContactForm()
    if request.form == 'POST':
        if form.validate == False:
            flash('Fill all the fields')
            return render_template('contact.html', form=form)
        else:
            return 'Success'
    else:
        return render_template('contact.html', form=form)
...

forms.py

from flask_wtf import Form
from wtforms import StringField, IntegerField, TextAreaField, SubmitField, RadioField, SelectField, EmailField
from wtforms import validators, ValidationError


class ContactForm(Form):
    name = StringField('Name of student', [validators.data_required('Please enter your name')])
    gender = RadioField('Gender', choices=[('M', 'Male'), ('F', 'Female')])
    address = TextAreaField('Address')
    email = EmailField('Enter your email', [validators.data_required('Please enter your email')])
    age = IntegerField('Age')
    language = SelectField('Languages', choices=[('cpp', 'C  '), ('py', 'Python')])
    submit = SubmitField('Send')

contact.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact form</title>
</head>
<body>
    <h1 style="text-align: center;" >Contact Form</h1>
    
    {% for message in form.message.errors %}
    <div>{{ message }}</div>
    {% endfor %}
    
    {% for message in form.email.errors %}
    <div>{{ message|e }}</div>
    {% endfor %}
    
    <form action="/contact" method="post">
        <fieldset>
            <legend>Contact Form</legend>
            {{ form.hidden_tag }}
            {{ form.name.lable }}
            {{ form.name }}
            <br>
            {{ form.gender.lable }}
            {{ form.gender }}
            <br>
            {{ form.address.lable }}
            {{ form.address }}
            <br>
            {{ form.email.lable }}
            {{ form.email }}
            <br>
            {{ form.age.lable }}
            {{ form.age }}
            <br>
            {{ form.submit }}
        </fieldset>
    </form>
    
</body>
</html>

form.message.errors

All part of my app is correctly work – thank you for advices

CodePudding user response:

flask_wtf.Form which subclasses wtforms.form.Form has a errors field. You want to access validation errors on the form in your template via it.

{% for field in form.errors %}
   <div>{{ field }}
   {% for message in form.errors[field] %}
       <div>{{ message }}</div>
   {% endfor %}
   </div>
{% endfor %}

To access the errors for a particular form field such as the name field, you can write:

{% for message in form.name.errors %}
   <div>{{ message }}</div>
{% endfor %}
  • Related