Majkelele
Majkelele

Reputation: 365

Flask jinja2.exceptions.UndefinedError: 'form' is undefined* [RESOLVED]

Edit: okey guys after three hourse i resolve this problem... problem was in html relied on **{{form.RegcheckEmial(name='email',placeholder='Email')}}** name='email' give me that erorr i resovled like this:

in reg.html

i removed

{{form.RegcheckEmial(name='email',placeholder='Email')}}

and replace this

{{form.RegcheckEmial(placeholder='Email')}}</br> 

in file.py

i removed:

Email=request.form['email']
username=request.form['username']
password=request.form['password']

and replaced this:

   email=form.RegcheckEmial.data   
   user=form.Regcheckuser.data
   password=form.Recheckpassword.data

dHi Guys i have problem with flask , console give me error such as i don't know how to solve this problem, my html says my form is undefined I want to check the validation of my input data and if the data has been entered successfully with validation, go to sending the data to my database classes

class LoginForm(FlaskForm):
    checkEmial = StringField('checkEmial', validators=[InputRequired('A username is required!'), Length(min=5, max=50, message='Must be between 5 and 10 characters.'),Email(message='PLS ENETER CORRET Email')])
    checkPassword = PasswordField('checkPassword', validators=[InputRequired('Password is required!'),Length(min=6, max=30, message='type the stronger password')])

class RegisterForm(FlaskForm):
          RegcheckEmial=StringField('checkkEmiall',validators=[InputRequired('Please type value'),Length(min=5, max=50, message='Must be between 5 and 10 characters.'),Email(message='PLS ENETER CORRET Email')])

File.py

@app.route('/reg.html',methods=['POST'])
def Register():
    form=RegisterForm()        
    if form.validate_on_submit():
      if request.method=='POST':         
       Email=request.form['email']
   username=request.form['username']
   password=request.form['password']
            
   cur=mysql.connection.cursor()
   cur.execute('''INSERT INTO dane (Email,username,UserPassword) VALUES (%s , %s , %s)''',(Email , username , password))
   mysql.connection.commit() 
   cur.close()
   return redirect(url_for('index'))
   return render_template('reg.html',form=form)

reg.html

<div class="LoginSquare">
{% block content %}
    <form method='POST' >




                                    
{{ form.csrf_token }}
{{form.RegcheckEmial(name='email',placeholder='Email')}}  \
{% for error in form.RegcheckEmial.errors %}
<span style="color:red;">{{ error }}</br></span>
{% endfor %}
<div class="d1">
<input type='submit'name='Register' id='awdwd'class='wdawd'>
</div>
</form>
{%endblock%}

**index.html//(

this is my login page where I use LoginForm classes )**

 <form method='POST' action="{{ url_for('index') }}" >
                              <div class="page1">
                                    
                                    <img  src="/static/pages/useerr.png" width="50px" height="50px">
                              
                              </div>
                              <!-- {{form.csrf_token}} -->
                             
                              {{ form.csrf_token }}
                             
                              {{ form.checkEmial(placeholder='Email')}}</br>
                              
                              {% for error in form.checkEmial.errors %}
                                  <span style="color:red;">{{ error }}</br></span>
                              {% endfor %}

error console : jinja2.exceptions.UndefinedError: 'form' is undefined,in top-level template code {% block content %}, File "c:\python39\lib\site-packages\jinja2\environment.py", line 474, in getattr return getattr(obj, attribute)

Upvotes: 0

Views: 1498

Answers (1)

charchit
charchit

Reputation: 1512

You have two problems in code.

  1. you don't have to use the filename as the route. It can be like "/register" or "/signup" and You will be able to access page by going to url like www.site.com/register if you use /register as route.
  2. You have only allowed post request to the url and you will not be able to visit the site. It will give you error 405 method not allowed error. You need to allow get request to url.

So your route should be:

@app.route('/register',methods=['POST','GET'])

You can go to yoursite.com/register to access the page.

Also I am assuming that you are visiting a wrong url as you haven't provided in your question where you haven't provided form variable to template.

Upvotes: 1

Related Questions