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>

红框内必须添加 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huanghong6956

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值