zoukankan      html  css  js  c++  java
  • Flask第一个实例

    Flask实现

    from flask import Flask,render_template,request,redirect,session
    
    app = Flask(__name__) # 一个Flask类的对象
    app.secret_key = 'u2jksidjflsduwerjl'
    app.debug = True
    USER_DICT = {
        '1': {'name':'志军','age':18},
        '2': {'name':'大伟','age':48},
        '3': {'name':'梅凯','age':38},
    }
    
    @app.route('/login',methods=['GET',"POST"])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        user = request.form.get('user') # 获取POST传过来的值
        pwd = request.form.get('pwd') # 获取POST传过来的值
        if user == 'alex' and pwd == '123':
            # 用户信息放入session
            session['user_info'] = user
            return redirect('/index')
        else:
            return render_template('login.html',msg ='用户名或密码错误')
            # return render_template('login.html',**{'msg':'用户名或密码错误'})
    
    
    @app.route('/index',endpoint='n1')
    def index():
        user_info = session.get('user_info')
        if not user_info:
            return redirect('/login')
    
        return render_template('index.html',user_dict = USER_DICT)
    
    @app.route('/detail')
    def detail():
        user_info = session.get('user_info')
        if not user_info:
            return redirect('/login')
    
        uid = request.args.get('uid')
        info = USER_DICT.get(uid)
    
        return render_template('detail.html',info = info)
    
    
    @app.route('/logout')
    def logout():
        del session['user_info']
        return redirect('/login')
    
    
    if __name__ == '__main__':
        app.run()

    templateslogin.html

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>休息系统-用户登录</title>
    </head>
    <body>
        <h1>登录</h1>
        <form method="post">
            <input type="text" name="user">
            <input type="password" name="pwd">
            <input type="submit" value="提交"> {{msg}}
        </form>
    </body>
    </html>

    templatesindex.html

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
    </head>
    <body>
        <ul>
            {% for k,v in user_dict.items() %}
                <li>{{v.name}} <a href="/detail?uid={{k}}">查看详细</a></li>
            {% endfor %}
        </ul>
    </body>
    </html>

    templatesdetail.html

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
    </head>
    <body>
        <h1>详细信息</h1>
        <div>{{info.name}}</div>
        <div>{{info.age}}</div>
    </body>
    </html>

    app.run=>run_simple

    from werkzeug.wrappers import Request, Response
    from werkzeug.serving import run_simple
    
    
    @Request.application
    def hello(request):
        return Response('Hello World!')
    
    if __name__ == '__main__':
        # 请求一旦到来,执行第三个参数    参数()
        run_simple('localhost', 4000, hello) # hello(xxx)
    天道酬勤 循序渐进 技压群雄
  • 相关阅读:
    ArcGis Engine中实现对符号的预览图输出
    重置IE为系统默认浏览器和VS2005的默认浏览器
    符号库制作过程说明
    关于arcgis engine符号库的讨论
    ArcGIS平台上的完美符号化插件方案
    Windows下的所有运行命令,你知多少?
    MapObjects地图填充符号库扩展
    用iframe 自动适应高度
    完美的C++:C++/CLI
    免费顶级CN域名,现在是每天10个
  • 原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/14857761.html
Copyright © 2011-2022 走看看