Why my form on FlaskForm returning an UnboundField?

Issue

everyone!
I’m writing my firsts rows on an HTML code to create a website with Flask using Python on VSCode. But, today I stopped and can’t solve a problem with my login or signup form.
When I try to show my form on my page, this message appears instead the fields and variate for all types: <UnboundField(StringField, ('Nickname',), {'validators': [<wtforms.validators.DataRequired object at 0x0000000003A8D048>]})>

It’s my GitHub page to look deep:
https://github.com/Kaindall/Python-Flask_Website

And it’s my code:

My form page:

from wtforms import StringField, PasswordField, SubmitField, Form
from wtforms.validators import DataRequired, Length, Email, EqualTo


class FormCreateAcc(FlaskForm):
    username = StringField('Nickname', validators=[DataRequired()])
    email = StringField('E-mail adress', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    pw_confirmation = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
    submit_createaccount = SubmitField('Create Account')

class FormLogin(FlaskForm):
    email = StringField('E-mail', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    submit_login = SubmitField('Login')

It’s main page managing all the website:

from flask import Flask, render_template, url_for
from forms import FormCreateAcc, FormLogin

app=Flask(__name__)
app.config['SECRET_KEY'] = 'I15NxqBiJ7R9nnUWhM3Nd5dCu3TUM8su'

user_list = ['Jax', 'Fiora', 'Gnar', 'Khazix', 'Viktor']


@app.route('/')
def home():
    return render_template('homepage.html')

@app.route('/contact')
def contact():
    return render_template('contact.html')

@app.route('/users')
def users():
    return render_template('users.html', user_list=user_list)

@app.route('/login')
def login():
    f_login = FormLogin()
    f_createacc = FormCreateAcc()
    return render_template('login.html', f_login=FormLogin, f_createacc=FormCreateAcc)




if __name__ == '__main__':
    app.run(debug=True)

And, finally, it’s my page trynna show my form:

{% extends 'default.html' %}

{% block body %}

<div class="container">
    <div class="row">
        <div class='col' id="login_camp">
            <h1><strong> Login </strong></h1>
            {{ f_login.email }}
            <hr>
            {{ f_login.password }}
        </div>
        <div class='col' id="signup_camp">
            <h1><strong>Create Account</strong></h1>
            {{ f_createacc.username }}
            {{ f_createacc.email }}
            {{ f_createacc.password }}
            {{ f_createacc.pw_confirmation }}
        </div>
    </div>
</div>
{% endblock %}

Obs: My default.html doesn’t change anything on the code, is almost a empty block of code just to can padronize all pages. \

Thx you so much just for reading. And a little bit more if wanna answer.

Solution

return render_template('login.html', f_login=FormLogin, f_createacc=FormCreateAcc)

This line of code distributes your variable f_login incorrectly to the html.

For return render template you are setting the name of variable in jinja and the value of it in python

So

return render_template("login.html", f_createacc = FormCreateAcc)

Sets a jinja variable f_createacc with a value of FormCreateAcc (which is a class)

You can do this instead

@app.route('/login')
def login():
    f_login = FormLogin()
    f_createacc = FormCreateAcc()
    return render_template('login.html', f_login=f_login, f_createacc=f_createacc)

Answered By – Winmari Manzano

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published