操作平台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 %}

  

Python>>>Flask框架使用之入门的更多相关文章

  1. Python之Flask框架项目Demo入门

    Python+Flask框架项目Demo入门 本例子用到了 Flask+蓝图+Flask-Login+SQLAlchemy+WTForms+PyMySQL相关架构 Flask Web框架介绍 Flas ...

  2. Python 什么是flask框架?快速入门

    一:Python flask框架 前言 1.Python 面向对象的高级编程语言,以其语法简单.免费开源.免编译扩展性高,同时也可以嵌入到C/C++程序和丰富的第三方库,Python运用到大数据分析. ...

  3. Linux ubantu中安装虚拟/使用环境virtualenv以及python flask框架

    今天学习了python flask框架的安装过程以及使用案例,感觉网上讲的东西都没有从我们这种初学者的角度去考虑(哈哈),最后还是奉上心得: 1.安装virtualenv $ sudo apt-get ...

  4. python flask框架学习——开启debug模式

    学习自:知了课堂Python Flask框架——全栈开发 1.flask的几种debug模式的方法 # 1.app.run 传参debug=true app.run(debug=True) #2 设置 ...

  5. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  6. python flask框架学习(一)——准备工作和环境配置与安装

    Flask装备: 学习自:知了课堂Python Flask框架——全栈开发 1.Python版本:3.6 2.Pycharm软件: 3.安装虚拟环境: (1)安装virtualenv: pip ins ...

  7. #3使用html+css+js制作网页 番外篇 使用python flask 框架 (II)

    #3使用html+css+js制作网页 番外篇 使用python flask 框架 II第二部 0. 本系列教程 1. 登录功能准备 a.python中操控mysql b. 安装数据库 c.安装mys ...

  8. #3使用html+css+js制作网页 番外篇 使用python flask 框架 (I)

    #3使用html+css+js制作网页 番外篇 使用python flask 框架(I 第一部) 0. 本系列教程 1. 准备 a.python b. flask c. flask 环境安装 d. f ...

  9. Python Flask框架路由简单实现

    Python Flask框架路由的简单实现 也许你听说过Flask框架.也许你也使用过,也使用的非常好.但是当你在浏览器上输入一串路由地址,跳转至你所写的页面,在Flask中是怎样实现的,你是否感到好 ...

随机推荐

  1. Java 数组声明与初始化

    引言 学习了好久的java,每次要写数组的声明和初始化代码,总是理不清.最近又碰到了一次这种情况.这次拿出<Thinking In Java>好好总结一翻. 数组声明 对于数组的声明其实都 ...

  2. C#中Dictionary<TKey,TValue>排序方式

    自定义类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...

  3. 安全协议系列(五)---- IKE 与 IPSec(中)

    在上一篇中,搭建好了实验环境.完整运行一次 IKE/IPSec 协议,收集相关的输出及抓包,就可以进行协议分析.分析过程中,我们将使用 IKE 进程的屏幕输出和 Wireshark 抓包,结合相关 R ...

  4. 【Python】个人所得税

    以月收入1w,举例计算个税: #!/usr/bin/python #-*- encoding:UTF-8 -*- #========================================== ...

  5. 使用恶意USB设备解锁 Windows & Mac 锁屏状态

    NSA专业物理入侵设备——USB Armory,可解锁任意锁屏状态的下的Windows和Mac操作系统,含最新发布的Windows10.及较早的Mac OSX El Capitan / Maveric ...

  6. spring 3.0 应用springmvc 构造RESTful URL 详细讲解

    在线springmvc_rest demo 由于下一版本的rapid-framwork需要集成spring RESTful URL,所以研究了一下怎么搭建. 并碰到了一下问题. springmvc 3 ...

  7. python bytes to string

    python bytes 转化成 string 会遇到如下错误: codec can't decode byte 0xff in position 5: illegal multibyte seque ...

  8. 用递归法判断字符串A中包含多少个字符串B

    string类提供了判断字符串B在字符串A中首次(或最后)出现的Index的方法,但有时候需要判断B在A中出现了多少次. 为此想了一个算法. public static void CountIndex ...

  9. android view : 绘制

    说到绘制,其实就是如何把一个view的对象,变成手机上可视的图形.很多人总结3个过程:测量,布局,绘制.这也是所有的要显示图形的程序所应该抽象的3个步骤,测量就是测量出你view的大小,布局就是要显示 ...

  10. Guid与id区别

    一.产生Guid的方法 1.SqlServer中使用系统自带的NEWID函数: select NEWID() 2.C#中,使用Guid类型的NewGuid方法: Guid gid;           ...