How to solve the form is undefined , Flask?

Question:

I have two routes inside my application, if i want to access the second one i got this error:

UndefinedError: 'register_form' is undefined

The register_form is for the first route, here is the first route:

@abonent_route.route('/')
@abonent_route.route('/', methods=['GET', 'POST'])
def index():

    register_form = RegistrationForm()
    return render_template('index.html', register_form=register_form)

This is the second route:

@client_route.route('/client/')
@client_route.route('/client/', methods=['GET', 'POST'])
def index():
    form_login = ClientLogin()
    return render_template('index.html', form_login=form_login)

Also I’ve initialized the routes inside my create_app function as following:

from .abonent import abonent_route
app.register_blueprint(abonent_route)

from .client import client_route
app.register_blueprint(client_route)

Now the problem is if i replaced the client route step up so to be above the abonent route and if i open abonent route, i got the same error but a little bit different:

UndefinedError: 'form_login' is undefined

This time, the form_login is undefined, also if i again moved the abonent back again to it place and if i want to visit the client route, i got the first error which is register_form undefined .

Please any help would be appreciated .

Edit: Add some codes

Client main route:

@client_route.route('/client/', methods=['GET', 'POST'])
def index():
    form_login = ClientLogin()
    if request.method == 'GET' and request.args.get('next'):
        session['next'] = request.args.get('next')

    if form_login.validate_on_submit():
        user = Client.query.filter_by(
            tele = form_login.telephone.data
        ).first()
        if user:
            if check_password_hash(user.password, form_login.password.data):
                session['client_logged_in'] = user.name
                session['client_family'] = user.family
                session['client_image'] = user.image
                session['client_phone'] = user.tele
                if 'next' in session:
                    next = session.get('next')
                    session.pop('next')
                    return redirect(next)
                else:
                    flash('Привет, {}'.format(user.name), 'success')
                    return redirect(url_for('client.profile'))
            else:
                flash('Неверные учетные данные.', 'danger')
                return redirect(url_for('client.index'))
        else:
            flash('Неверные учетные данные.', 'danger')
            return redirect(url_for('client.index'))
    return render_template('index.html', form_login=form_login)

Abonent main route:

@abonent_route.route('/', methods=['GET', 'POST'])
def index():
    register_form = RegistrationForm()
    if request.method == 'POST':
        if register_form.is_submitted():
            if not register_form.terms_agree.data:
                flash('Вы должны согласиться с нашим договором и со всеми его пунктами.', 'danger')
                return redirect(url_for("abonent.index"))
            if register_form.master_salon.data or register_form.master_cto.data or register_form.master_company.data == True:
                user = User()
                user.name = register_form.name.data
                user.family = register_form.family.data
                user.bio = register_form.biography.data
                if User.query.filter_by(tele=register_form.telephone.data).first():
                    flash('Этот номер: {} уже использован.'.format(register_form.telephone.data), "warning")
                    return redirect(url_for('abonent.index'))
                else:
                    user.tele = register_form.telephone.data
                user.set_password(register_form.password.data)
                db.session.add(user)
                db.session.commit()
    return redirect(url_for('abonent.confirm', phone=phone))
return render_template('index.html', register_form=register_form)

Here is a link to where full error shows, this one is raises if the client route is under abonent route in __init__.py file:

Click to open

Another thing that i forgot to mention is, if tried to open for example /client/registration, it opens without any error, the error raises if i wanted to access /client which is the index page for client

Asked By: reznov11

||

Answers:

Seems like there is a couple of redundant block info in your code.

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

You don’t need twice to declare this route. The bottom line will be enough.

Also I’ve initialized the routes inside my create_app function as
following:

It is not declaring route. It’s a blueprint. It’s like big part of your project.
I can have many routes in your blueprint.
You don’t need declare one blueprint for one route.

For a more detailed answer, please provide a more detailed source code of your application.

Take a look:
http://exploreflask.com/en/latest/blueprints.html and
DO docs

Answered By: Vladimir Yakovenko

I must be kidding with myself Hahahaha, i solved the problem.

Inside __init__.py file in my client folder i configured the route as following:

from flask import Blueprint

client_route = Blueprint(
    'client',
    __name__,
    template_folder='templates/client',
    static_folder='../static'
)

from . import views

As you can see here, index.html must be inside client folder, when i rendered index.html inside my views i forgot to add client/ before index.html here :

return render_template('client/index.html', form_login=form_login)

It was looking in my main app templates folder where another index.html is saved .

Now everything is working very well:) , thank you all guys for your interests .

Answered By: reznov11
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.