1

For context, I'm trying to create a flask-wtf form. I've done that and it's presented correctly:

class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    submit = SubmitField('Sign In')

@APP.route('/')
def index():
    return render_template('login.html', form=LoginForm())

@APP.route('/login', methods=['POST', 'GET'])
def login():
    print(request.method, request.get_json())
    return redirect('/')  # temporary

Here's the html:

{% block content %}
    <h1>Sign In</h1>
    <form action="/login" method="post" enctype='application/json'>
        {{ form.hidden_tag() }}
        <p>
            {{ form.username.label }}<br>
            {{ form.username(size=32) }}
        </p>
        <p>
            {{ form.password.label }}<br>
            {{ form.password(size=32) }}
        </p>
        <p>{{ form.submit() }}</p>
    </form>
{% endblock %}

However, when I enter anything and click submit, what gets printed from login() is POST None. I don't understand why there's no data being sent. As an attempt to force it to be json, I added enctype='application/json' and nothing before or after.

What am I missing?

1 Answer 1

1

You can't POST a form as JSON, there are only 3 options for enctype.

In your login() function you can access the form through request.form, or when using wtforms, initialise the form in your view for hte data to be populated:

def login():
    login_form = LoginForm()

    if login_form.validate_on_submit(): # this runs only on POST, and checks validation
        username = form.username.data
        password = form.password.data
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

You're missing an if in there if I'm not mistaken. In any case, thank you!
Good catch, fixed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.