zoukankan      html  css  js  c++  java
  • flask基础1

    1、初识flask

    from flask import Flask   #最简单的一个项目就这么启动了
    
    app=Flask(__name__)   #实例化一个flask
    
    @app.route('/')           #路由
    def index():                   #函数
        return "123"
    
    
    if __name__=="__main__":             #入口
        app.run()

    2、模板和跳转

    这里的请求request过来的时候不是通过参数传过来的,而是通过模块直接导入

    from flask import Flask,render_template,request,redirect
    
    app=Flask(__name__,template_folder="templates")
    
    @app.route('/')
    def index():
        return "123"
    
    @app.route("/login",methods=["GET","POST"])
    def login():
        if request.method =="GET":
            return render_template("login.html")
        username=request.form.get("username")
        password=request.form.get("password")
        if username =="zhangsan" and password == "123":
            return redirect("index.html")
    
    if __name__=="__main__":
        app.run()

    3、session的使用

    在使用session的时候要注意加盐

    导入直接使用就可以了

    from flask import Flask,render_template,request,redirect,session
    
    # app = Flask(__name__,template_folder="templates",static_folder="staticccc",static_url_path='/vvvvv')
    app = Flask(__name__,template_folder="templates",static_folder="static")
    app.secret_key = 'asdfasdf'
    
    
    
    @app.route('/login',methods=["GET","POST"])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        user = request.form.get('user')
        pwd = request.form.get('pwd')
        if user == 'oldboy' and pwd == '666':
            session['user'] = user
            return redirect('/index')
        return render_template('login.html',error='用户名或密码错误')
        # return render_template('login.html',**{"error":'用户名或密码错误'})
    
    @app.route('/index')
    def index():
        user = session.get('user')
        if not user:
            return redirect('/login')
        return render_template('index.html')
    
    if __name__ == '__main__':
        app.run()
  • 相关阅读:
    DP问题之最长非降子序列
    CentOS 6.8 编译安装MySQL5.5.32
    [Linux] killall 、kill 、pkill 命令详解
    编写登陆接口
    python学习day01
    python购物车程序
    ERROR:Attempting to call cordova.exec() before 'deviceready'
    BSF脚本引擎‘改变’Bean
    Solr安装配置
    amchart配置备忘
  • 原文地址:https://www.cnblogs.com/wangyuxing/p/9169005.html
Copyright © 2011-2022 走看看