flask学习记录-遇到的各种报错

1.启动时遇到urls must start with a leading slash报错

解决方法

    from flask import render_template

    @app.route('/hello/') # 注意这里网页前有/
    @app.route('/hello/<name>')
    def hello(name=None):
        return render_template('hello.html', name=name)

解决方法: 注意在hello前面需要加上/

 

2.实例化wtforms时遇到RuntimeError: Working outside of application context.报错

执行如下代码时报错

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, IntegerField, \
    TextAreaField, SubmitField, MultipleFileField
from wtforms.validators import DataRequired, Length, ValidationError, Email


# 4.2.1 basic form example
class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired(), Length(8, 128)])
    remember = BooleanField('Remember me')
    submit = SubmitField('Log in')


>>> form1 = LoginForm()

未能找到解决办法,改为执行python app.py, 在如下函数中调试

#测试用例
@app.route('/hi')
def hi():
    form = LoginForm()
    print(form.username())

    return 'hi'

浏览器访问hi时, 可在cmd界面内打印如下内容

<input id="username" name="username" required type="text" value="">
127.0.0.1 - - [06/Jun/2022 14:16:10] "GET /hi HTTP/1.1" 200 -

 

posted @ 2022-04-14 11:26  毛绒绒  阅读(276)  评论(0编辑  收藏  举报