1

I write a site on Flask, I connected flask-security, at first I changed the authorization template to the standard flask-security authorization template, I found a way to solve this problem, but now, as soon as I add a form to my template to work with WTF- forms, the error

jinja2.exceptions.UndefinedError: 'form' is undefined appears

if I make some simple template, then everything seems to be fine, although the parameter form=form is specified in my view function. Here is my html file for this page(templates/security/login_user.html)

{% extends 'base.html' %}

{% block body %}
{{ super() }}
    {% for cat, msg in get_flashed_messages(True) %}
        <div class="flash {{cat}}">{{msg}}</div>
    {% endfor %}
        <div class="container">
            <form class="box" method="POST">
                {{ form.hidden_tag() }}
                <h1 title="Авторизуйтесь,будь ласка">Авторизація</h1>
                <div class="group">
                    <label>{{ form.username.label }}</label>
                    {{ form.username }}
                </div>
                <div class="group">
                    <label>{{ form.email.label }}</label>
                    {{ form.email }}
                </div>
                <div class="group">
                    <label>{{ form.password.label }}</label>
                    {{ form.password }}
                </div>
                <div class="group">
                    <center><button>Авторизуватися</button></center>
                </div>
                <a href="{{ url_for('reset_password') }}">Забули пароль?</a>
            </form>
        </div>
{% endblock %}

This is my view-function to this page

@app.route('/login', methods=['POST', 'GET'])
def login_page():
    form = AuthorizationForm()
    if form.validate_on_submit():
        username = form.username.data
        email = form.email.data
        password = form.password.data
        if username and email and password:
            user = User.query.filter_by(username=username, email=email).first()
            if check_password_hash(user.password, password):
                login_user(user)
                return redirect(url_for('index_page'))
            else:
                flash('Неправильний логін або пароль', category='error')
        else:
            flash('Заповніть,будь ласка,всі поля', category='error')

    return render_template('security/login_user.html', title='Авторизація', form=form,
                           css_link=css_file_authorization)

This is my class to WTFForm

class AuthorizationForm(FlaskForm):
    username = StringField('Введіть свій username: ')
    email = StringField('Ваш email: ')
    password = PasswordField('Введіть свій пароль: ')

And in the documentation, I saw that it is possible to write it here for connecting the template:

SECURITY_LOGIN_USER_TEMPLATE = '/templates/security/login_user.html'

But it doesn`t help

Dimaapp
  • 123
  • 1
  • 1
  • 7

1 Answers1

1

I would suggest adding a name=form attribute to the <form element, and see if that helps. Flask-Security actually uses a unique name for the form - in the view it sets e.g. login_form=form and in the template the form element has 'name=login_form' and to reference members in the template login_form.username.

jwag
  • 662
  • 5
  • 6
  • As far as I understand, I need to do this in
    ````
    ``` And everywhere in the template, use ```login_form``` instead of ```form```, for example like this:```{{ login_form.username }}``` And in the view, connect like this ```return render_template('security/login_user.html',title='Авторизація', login_form=form, css_link=css_file_authorization)```
    – Dimaapp Jul 16 '22 at 20:25