flask-安装-css样式加载-cookie-session设置-上传图片文件


from flask import Flask
app=Flask(__name__)

#开启debug模式
app.config['DEBUG']=True

@app.route('/')
def index():
    return '你好'

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

最简单的flask页面

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'
#开启debug模式
app.config['DEBUG']=True

@app.route('/test/')
def testd():
    # 跳转页面-HTML需要建在templates文件夹下
    return render_template('test.html')

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    #添加css样式文件 建在static文件夹下css下
    <link rel="stylesheet" href="{{ url_for('static',filename='css/style.css',_external=True )}}">

</head>
<body>
测试
</body>
</html>

from flask import Response
@app.route('/setcookie/')
def setcookie():
    #获取响应对象
    response=Response('cookie设置成功')
    #设置cookie
    response.set_cookie('cooktest','123',max_age=7200)
    return response

设置cookie

#设置session
import os
from flask import session
app.secret_key=os.urandom(24)

#session设置成功
@app.route('/setsession/')
def setsession():
    session['test']='123'
    return 'session设置成功'
#获取session
@app.route('/getsession/')
def getsession():
    return session['test']

登录验证回显

html='''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/denglu2/" method="POST">
    <input type="text" name='zhanghao' value={0}>
    <input type="password" name='mima'>
    <button type="submit">登录</button>
</form>

</body>
</html>
'''
from flask import request

@app.route('/denglu/',)
def denglu():

        return html

@app.route('/denglu2/',methods=['GET','POST'])
def denglu2():
    if request.method=='POST':
        zhanghao = request.values.get('zhanghao')
        mima = request.values.get('mima')
        if zhanghao=='admin' and mima=='123':
            print('验证成功')

            return html.format(zhanghao)
        else:
            return html
    else:
        return html

上传图片文件

import uuid
@app.route('/shangchuan/',methods=['GET','POST'])
def shangchuan():
    if request.method=='POST':
#提取文件名
        filed=request.files['wenjian']
        named=filed.filename
        typed=named.split('.')[-1]
        print(filed.filename)
#验证文件类型
        alltype=['jpg','png','bmp','gif']
        if typed in alltype:
            print(typed)
            uploadpath=os.getcwd()+os.sep+'static/file'
#创建文件夹
            if not os.path.exists(uploadpath):
                os.mkdir(uploadpath)
#uuid命名不重复姓名
            named=str(uuid.uuid1())+'.'+typed
            filed.save(uploadpath+os.sep+named)
            return html.format('上传成功')
        else:
            return html.format('')
    else:
        return html.format('')

<form action="/shangchuan/" method="post" enctype="multipart/form-data">

<input type="file" name="wenjian">
<button type="submit">上传</button>

</form>

红框内必须添加 

猜你喜欢

转载自blog.csdn.net/huanghong6956/article/details/84967712