ggordon
ggordon

Reputation: 259

Flask WTF Form Validation Error

I am new to Flask and am trying to take a user entered name from a login form and pass it to another page. With my current code, the form does not pass the result page any argument and returns none when request.args.get('artistName') is called in the result view. What is wrong? print form.errors does not show anything.

The views.py programme:

from flask import render_template, request, url_for, redirect

from app import app
from .forms import UserName

@app.route('/', methods=['GET','POST'])
def login():
    form = UserName()

    if form.validate_on_submit():
        return redirect('/result')

    render_template('home.html',form=form)

@app.route('/result', methods=['POST', 'GET'])
def result(name):
    return render_template('results.html')

The home.html:

{% extends "base.html" %}

{% block content %}
  <form action="{{ url_for('result') }}" method='post'>
    {{ form.csrf_token }}
    {{ form.hidden_tag }}
    <p>Enter your artist name: </p>
    {{ form.artistName(size=100) }}
    {{ form.submit }}
  </form>
{% endblock %}

And forms.py:

from flask_wtf import FlaskForm
from wtforms import TextField, SubmitField
from wtforms.validators import DataRequired

class UserName(FlaskForm):
    artistName = TextField('artistName', [DataRequired()])
    submit = SubmitField("Go")

I would really appreciate any help!

Upvotes: 1

Views: 1193

Answers (1)

EoS
EoS

Reputation: 1

There should be a return statement in your login view:

return render_template('home.html',form=form)

Upvotes: 0

Related Questions