Flask之config.py配置文件中的常用配置

一、config.py中的常用配置

   'DEBUG':                                get_debug_flag(default=False),  是否开启Debug模式
    'TESTING':                              False,                          是否开启测试模式
    'PROPAGATE_EXCEPTIONS':                 None,                          
    'PRESERVE_CONTEXT_ON_EXCEPTION':        None,
    'SECRET_KEY':                           None,
    'PERMANENT_SESSION_LIFETIME'<
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件结构如下: ``` - app.py - config.py - requirements.txt - templates/ - base.html - index.html - register.html - login.html - test.html ``` 其,`app.py` 为主要的 Flask 应用程序代码,`config.py` 存放配置信息,`requirements.txt` 存放项目依赖。 `templates/` 文件夹,`base.html` 为页面的基本模板,其他模板都继承于它。 代码内容如下: `app.py`: ```python from flask import Flask, render_template, redirect, url_for, flash, request from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired, EqualTo from werkzeug.utils import secure_filename import os app = Flask(__name__) app.config.from_pyfile('config.py') class RegistrationForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')]) submit = SubmitField('Register') class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) submit = SubmitField('Login') @app.route('/') def index(): return render_template('index.html') @app.route('/register', methods=['GET', 'POST']) def register(): form = RegistrationForm() if form.validate_on_submit(): # save registration information to database flash('Registration successful') return redirect(url_for('login')) return render_template('register.html', form=form) @app.route('/login', methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): # check login information against database flash('Login successful') return redirect(url_for('test')) return render_template('login.html', form=form) @app.route('/test', methods=['GET', 'POST']) def test(): if request.method == 'POST': f = request.files['file'] filename = secure_filename(f.filename) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # do face detection on the uploaded image return render_template('test.html', filename=filename) return render_template('test.html') if __name__ == '__main__': app.run() ``` `config.py`: ```python UPLOAD_FOLDER = 'uploads' SECRET_KEY = 'secret_key' ``` `base.html`: ```html <!DOCTYPE html> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <nav> <ul> <li><a href="{{ url_for('index') }}">Home</a></li> <li><a href="{{ url_for('register') }}">Register</a></li> <li><a href="{{ url_for('login') }}">Login</a></li> </ul> </nav> {% with messages = get_flashed_messages() %} {% if messages %} <ul> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} {% block content %}{% endblock %} </body> </html> ``` `index.html`: ```html {% extends 'base.html' %} {% block title %}Home{% endblock %} {% block content %} <h1>Welcome to the Online Face Detection Test</h1> {% endblock %} ``` `register.html`: ```html {% extends 'base.html' %} {% block title %}Register{% endblock %} {% block content %} <h1>Register</h1> <form method="POST"> {{ form.csrf_token }} {{ form.username.label }} {{ form.username }} {{ form.password.label }} {{ form.password }} {{ form.confirm_password.label }} {{ form.confirm_password }} {{ form.submit }} </form> {% endblock %} ``` `login.html`: ```html {% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h1>Login</h1> <form method="POST"> {{ form.csrf_token }} {{ form.username.label }} {{ form.username }} {{ form.password.label }} {{ form.password }} {{ form.submit }} </form> {% endblock %} ``` `test.html`: ```html {% extends 'base.html' %} {% block title %}Test{% endblock %} {% block content %} <h1>Test</h1> <form method="POST" enctype="multipart/form-data"> {{ form.csrf_token }} <input type="file" name="file"> <input type="submit" value="Submit"> </form> {% if filename %} <img src="{{ url_for('static', filename='uploads/' + filename) }}"> {% endif %} {% endblock %} ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值