Apologies if this isn't worded very well. I can't seem to get any error messages to show when I haven't fulfilled the validation requirements. When I submit, the form is just refreshed without error messages such as "This field is required".
Also, when I do input data, if form.validate_on_submit():
doesn't return the expected homepage, it once again just refreshes the page. I'm aware the form does nothing, I plan on adding the data submitted to a database but I'm stuck on this step. Can anybody help? Thanks.
app.py
from cs50 import SQL
from flask import Flask, render_template, session, redirect
from flask_session import Session
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField #bool for checkbox
from wtforms.validators import InputRequired, Email, Length
# configure application
app = Flask(__name__)
app.config["SESSION_PERMENANT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Allows webpage to update html on refresh
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config['SECRET_KEY'] = 'Itsasecret'
# configure CS50 library to use SQLite database
db = SQL("sqlite:///project.db")
# Usually separate routes, database and form stuff into different files and link together
class LoginForm(FlaskForm):
username = StringField('Username', validators=[InputRequired(), Length(min=4, max=15)])
password = PasswordField('Password', validators=[InputRequired(), Length(min=8, max=80)]) #80 is a special number (learn more)
remember = BooleanField('Remember me')
@app.route('/')
def homepage():
return render_template("homepage.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
return redirect("/")
return render_template("login.html", form=form)
login template
{% extends "layout.html" %}
{% from "_formhelpers.html" import render_field %}
{% set active_page = 'login' %}
{% block title %} Login {% endblock %}
{% block body %}
<div class="ls-main">
<form action="/login" method="POST">
{{ form.csrf_token }}
{{ render_field(form.username) }}
{{ render_field(form.password, autocomplete="off") }}
{{ render_field(form.remember) }}
<div class="ls-div">
<input class="ls-input, signinbutton" name="submit" type="submit" value="Submit">
</div>
</form>
</div>
{% endblock %}
_formhelpers.html
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
return render_template("/")
is invalid. I think you mean return redirect("/")
Of course you need from flask import Flask, render_template, session, redirect