如何在flasklogin中使用wtforms?

时间:2019-07-08 15:46:45

标签: python wtforms

我是编码的新手,我尝试使用flask,但是我不断收到相同的错误,但我不知道为什么。我得到的错误是这样的:

  

jinja2.exceptions.UndefinedError:'wtf'未定义

主要内容在这里:

from flask import Flask, render_template, request, make_response, redirect, url_for
from models import Loginform
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user

app = Flask(__name__)
app.config["SECRET_KEY"] = "1234567890"


@app.route("/")
def index():
    return (render_template("index.html"))


@app.route("/login")
def login():
    form = Loginform()
    return (render_template("login.html", form=form))


@app.route("/signup")
def signup():
    return (render_template("signup.html"))


@app.route("/dashboard")
def dashboard():
    return (render_template("dashboard.html"))


@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('index'))


if __name__ == "__main__":
    app.run(debug=True)

这是我创建要在login.html中使用的表单的主要地方

import os
from sqla_wrapper import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, length

db = SQLAlchemy(os.getenv("DATABASE_URL", "sqlite:///localhost.sqlite"))  # this connects to a database either on Heroku or on localhost


class Loginform(FlaskForm):
    name = StringField("username", validators=[InputRequired, length(min=4, max=15)])
    password = PasswordField("password", validators=[InputRequired, length(min=8, max=80)])
    remember = BooleanField("rememberme")

0 个答案:

没有答案