操作平台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. ubuntu安装谷歌输入法

    1,sudo apt-get install fcitx-googlepinyin 2,在settings->Language Support里将keyboard input method sy ...

  2. ionic cordova 热更新(引用自www.zyyapp.com/post/116.html)

    上篇文章cordova 把html打包成安卓应用 http://www.zyyapp.com/post/115.html cordova 热更新是一个大坑,我看了一天一夜才明白.网上的教程都没说到重点 ...

  3. Hadoop程序运行中的Error(1)-Error: org.apache.hadoop.hdfs.BlockMissingException

    15/03/18 09:59:21 INFO mapreduce.Job: Task Id : attempt_1426641074924_0002_m_000000_2, Status : FAIL ...

  4. 基于.NET的CAD二次开发学习笔记一:CAD开发入门

    1.AutoCAD .NET API由不同的DLL文件组成,它们提供用于访问图形文件或AutoCAD应用程序的包含丰富的类.结构.方法和事件.每一个DLL文件都定义不同的使用基于功能的库组织组件的命名 ...

  5. demo和实际项目的距离

    回家的路上想到一个很形象的类比,关于学生时期的实验(以及一些简单的demo)和实际工作项目的差别. 实现了同样的功能,比如要制作一把椅子,如果是简单的demo,那么就如同是给你了一个单独的房间,里面已 ...

  6. Spring的Bean的基本概念

    Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.Spring容器能够生产哪些产品,取决于配置文件的配置. 对于我们而言,使用Spring框架做两件事:开发Bean. ...

  7. hdu1004

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. amazon oa2 - insert a value into a cycled linked list

    遍历,一共有三种情况, 1. pre <= x <= current 2. 遍历到了head时,x>tail 或者 x<=head (不会等于tail) 3. 遍历回aNode ...

  9. Linux的课程总结

    Linux的课程总结 20125133 马国祥 通过16周的学习明白学好linux不是一件一蹴而就的事,一定要能坚持使用它,特别是在使用初期,由于在linux中,用户权限很大,做任何事情都很自由,所以 ...

  10. Html中代码换行造成空格间距的问题

    Html中代码换行造成空格间距的问题解析 解决方法: 一.简单粗爆不换行 写代码的时候不要换行,input等在一行输写,那么将解决该问题.但是代码就变得不再那么容易好看. 二.设置父级块的字体大小为0 ...