1. 程式人生 > >flask-安裝-css樣式載入-cookie-session設定-上傳圖片檔案

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>

紅框內必須新增