码迷,mamicode.com
首页 > 其他好文 > 详细

flask快速入门

时间:2020-02-03 13:38:00      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:rom   orm   file   request   port   url重定向   user   快速入门   load   

from flask import Flask,url_for, render_template
from flask import request, redirect

app = Flask(__name__)

# 路由
@app.route(/)
def index():
    return Index Page

# @app.route(‘/hello‘)
# def hello():
#     return ‘Hello World‘

# 变量规则
@app.route(/user/<username>)
def show_user(username):
    return User %s % username

# url重定向
@app.route(/project/)
def project():
    return project

@app.route(/about)
def about():
    return about


# http方法
# @app.route(‘/login‘, methods=[‘GET‘, ‘POST‘])
# def login():
#     if request.method == ‘POST‘:
#         return ‘do the login‘
#     else:
#         return ‘show the login form‘

# 模板渲染
@app.route(/hello/<name>)
def hello(name):
    return render_template(hello.html, name=name)


# 请求对象
@app.route(/login, methods=[GET, POST])
def login():
    if request.method == POST:
        username = request.form[username]
        password = request.form[password]
    else:
        return render_template(login.html)

# 文件上传
@app.route(/upload, methods=[GET, POST])
def upload_file():
    if request.method == POST:
        file_name = request.form[filename]
        f = request.files[the_file]
        f.save(/home/zhangjian/PycharmProjects/js逆向/app/file/{}.format(file_name))
        return render_template(upload.html)
    else:
        return render_template(upload.html)


# 重定向和错误
@app.route(/bbs)
def bbs():
    return redirect(url_for(upload_file))

if __name__ == __main__:
    app.run(127.0.0.1,5000)

 

flask快速入门

标签:rom   orm   file   request   port   url重定向   user   快速入门   load   

原文地址:https://www.cnblogs.com/zhangjian0092/p/12255204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!