宅神的博客

非商业 个人知识整理 欢迎转载

flask应用

flask

hello flask

mkdir flask-pro
cd flask-pro
mkdir hello-flask
cd hello-flask/
virtualenv venv --python=python3
source venv/bin/activate
pip install flask
mkdir app
touch app/__init__.py
touch app/routers.py
touch hello.py

app

# app/__init__.py

from flask import Flask

app = Flask(__name__)
from app import routers

Routers.py

from app import app

@app.route('/')
@app.route('/hello')
def index():
    return 'HELLO FLASK'
export FLASK_APP=hello.py
flask run

访问localhost:5000

hello flask

访问html

新建Hello.html

# app/templates/hello.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{ title }}</title>
</head>
<body>
    <h1>Hello,{{ user.name }}!</h1>
</body>
</html>

Routers.py

from app import app
from flask import render_template

@app.route('/')
@app.route('/hello')
def hi():
    user = {'name':'HOUSE','age':18}
    return render_template('hello.html',title='hello',user=user)

访问localhost:5000/

hello house

debug on

默认flask的debug模式是关闭的

开启方法一:app下新建config文件

DEBUG = true

应用内加上

app.config.from_object('config')

开启方法二:pycharm设置勾选flask_debug

pycharm

单页面应用

新建项目 hello-flask

下面创建目录templates

# hello-flask/app.py

from flask import Flask,request
# 注册app
app = Flask(__name__)

#路由+视图
@app.route('/login',methods=['GET','POST'])
def login():
    name = 'xiaoming'
    pwd = 'xiaoming'
    msg = None
    if request.method == 'POST':
        form = request.form
        username = form.get('username')
        password = form.get('password')
        if username == name and password == pwd:
            return '登陆成功'
        msg = '用户名或密码错误'
    from flask import render_template
    return render_template('login.html',msg=msg)


if __name__ == '__main__':
    app.run()

templates下建login.html

前端form表单

#templates/login.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
{% if msg %}
{{msg}}
{% endif %}
<form action="/login" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <button>登陆</button>
</form>
</body>
</html>

访问localhost:5000/login

login

失败返回

msg error

成功返回
success

url传参

#app.py
...
from werkzeug.routing import BaseConverter

class ReConverter(BaseConverter):
    def __init__(self,url,*args):
        self.url = url
        self.regex = args[0]
# url映射        
app.url_map.converters['re'] = ReConverter

@app.route('/test/<re(r"\d+"):page>')
def test_re(page):
    return page

访问localhost:5000/test/111
访问
———-持续更新—————

阅读更多
文章标签: python框架 flask
个人分类: Flask框架
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

flask应用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭