• Python>>>Flask框架使用之入门


    操作平台Windows Python2.7

    安装

    pip install flask

    Hello World程序

    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/hello')
    
    def hello_world():
        return "Hello World!!"
    if __name__ == '__main__':
        app.run()
    

    route装饰器是用于把一个函数绑定到一个 URL 上

    主页后面不同的URL

    @app.route('/')
    def index_page():
        return "this is the homepage"
    
    @app.route('/hello')
    def hello_world():
        return "Hello World!!"
    @app.route('/user')
    def user():
        return "this is the userspage"

     变量规则

    @app.route('/user/<username>')
    def show_user_profile(username):
        # 显示用户的名称
        return 'User %s' % username
    
    @app.route('/post/<int:post_id>')
    def show_post(post_id):
        # 显示提交整型的用户"id"的结果,注意"int"是将输入的字符串形式转换为整型数据
        return 'Post %d' % post_id

    静态文件放在 static目录中,模板文件放在templates目录下。

    from flask import Flask
    from flask import render_template
    app = Flask(__name__)
    
    @app.route('/hello')
    @app.route('/hello/<mypara>')
    def hello_world(mypara=None):
        return render_template('myhello.html',yourcontext=mypara)
    
    if __name__ == '__main__':
        app.run()

    这个py文件有个templates文件夹,里面有一个myhello.html

    内容如下

    <!doctype html>
    <title>Hello from Flask</title>
    {% if yourcontext %}
      <h1>Hello {{ yourcontext }}!</h1>
    {% else %}
      <h1>Hello World!</h1>
    {% endif %}

    yourcontext字符串会替代相关。

    一个简单登录网页

    本例子效果如下

    用户名和密码都正确(tianao)返回

    Hello tianao!

    否则返回

    Hello Invalid username/password!

    一共三个文件。主要的py文件,templates目录下的hello.html ,login.html

    from flask import Flask
    from flask import request
    from flask import render_template
    app = Flask(__name__)
    @app.route('/login', methods=['POST', 'GET'])
    def login():
        error = None
        if request.method == 'POST':
            if request.form['username'] == 'tianao' and request.form['password'] == 'tianao':
                return render_template('hello.html', name=request.form['username'])
            else:
                error = 'Invalid username/password'
        return render_template('login.html', error=error)
    if __name__ == '__main__':
        app.run()
    

      hello.html

    <!doctype html>
    <title>Hello from Flask</title>
    
    {% if name %}
      <h1>Hello {{ name }}!</h1>
    {% else %}
      <h1>Hello World!</h1>
    {% endif %}
    

      login.html

    <!doctype html>
    <title>Login Page</title>
    <form action="../login" method="POST">
        <input type="text" name="username" />
        <input type="password" name="password" />
        <input type="submit" value="submit" />
    </form>
    {% if error %}
      <h1>Hello {{ error }}!</h1>
    {% else %}
      <h1>Success!</h1>
    {% endif %}
    

      

  • 相关阅读:
    error MSB6006: ”cmd.exe” exited with code 1
    OpenGL简介
    OSG例程(1) 交互(Pick)
    $err,hr
    [转载]操作数的寻址方式
    严重推荐的图形学讲义
    编译通过,运行时osgDB::ReadImageFile()出错 d和非d的lib
    空间变换的顺序SRT
    OSG例程(3) 利用更新回调制作路径动画
    Visitor模式的C++实现
  • 原文地址:https://www.cnblogs.com/legion/p/6241378.html
Copyright © 2020-2023  润新知