zoukankan      html  css  js  c++  java
  • 小白flask框架第一个程序(windows平台下python2.7)

    步骤

    一、创建test文件夹

    二、在test文件夹下创建main.py文件和templates文件夹

      main.py代码如下:

      

     1 # -*- condig:utf-8 -*-
     2 
     3 from flask import Flask,render_template,request
     4 
     5 app = Flask(__name__)
     6 
     7 @app.route('/login')
     8 def index():
     9     # return 'hello Flask'
    10     return render_template('login.html')
    11     
    12 @app.route('/test',methods=['POST'])
    13 def success():
    14     if request.method == 'POST':
    15         email = request.form['email']
    16         return render_template('success.html',email=email)
    17     else:
    18         pass
    19     
    20 if __name__ == '__main__':
    21     app.run(
    22         host='0.0.0.0',
    23         port=7777,
    24         debug=True
    25     )
    View Code

    三、在templates文件夹下创建login.html文件和success.html文件

      login.html代码如下:

     1 <<!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta charset = utf-8>
     5 <title>flask登陆</title>
     6 </head>
     7 <body>
     8 
     9 <h1>Welcome to the Flask web</h1>
    10 <form action='/test' method='post'>
    11     <input type='text' name='email' placeholder='Enter Email Address'>
    12     <br/><br/>
    13     <input type='password' name='pass' placeholder='Passwork'>
    14     <input type='submit' value='Submit' name='ok'>
    15 
    16 </form>
    17 
    18 </body>
    19 </html>
    View Code

      success.html代码如下:

     1 <<!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta charset = utf-8>
     5 <title>登陆成功</title>
     6 </head>
     7 <body>
     8 
     9 <center><h2>SUCCESSFULLY WITH YOUR EMAIL: {{email}}</h2></center>
    10 
    11 
    12 </body>
    13 </html>
    View Code

    四、在cmd命令行下,切换到test文件夹下运行python2 main.py,

    在浏览器网址输入框内输入127.0.0.1:7777/login则转到登陆页面,

    在登陆页面输入email和password点击submit则转到登陆成功页面。

  • 相关阅读:
    Mongodb
    Java原子类
    volatile
    uniapp输入空格
    看不见的的html
    小程序隐藏scroll-view滚动条的方法
    云函数调用云函数 openid不存在
    vue路由中 Navigating to current location ("/xxx") is not allowed
    Vue: 单页面应用如何保持登录状态
    letter-spacing
  • 原文地址:https://www.cnblogs.com/znh8/p/9322517.html
Copyright © 2011-2022 走看看