笔记-flask基础操作

1.      前言

本文为flask基础学习及操作笔记,主要内容为flask基础操作及相关代码。

2.      开发环境配置

2.1.    编译环境准备

安装相关Lib

windows环境下:

cd ….venv/Scripts

active

pip install flask

2.2.    pycharm配置

  1. 在setting中的编译器中新增编译器(指向venv中的Python文件)
  2. 在run/debug configure中使用新增的编译器。

3.      项目创建

3.1.    项目文件组织结构设计:

一个比较良好的flask项目结构

project/

app/                    # 整个程序的包目录

static/                 # 静态资源文件

js/                     # JS脚本

css/                    # 样式表

img/                    # 图片

favicon.ico              # 网站图标

templates/              # 模板文件

common/                 # 通用模板

errors/                 # 错误页面

user/                   # 用户模板

posts/                  # 帖子模板

email/                  # 邮件发送

views/                  # 视图文件

models/                 # 数据模型

forms/                  # 表单文件

config.py               # 配置文件

email.py                # 邮件发送

extensions.py           # 各种扩展

migrations/             # 数据库迁移目录

tests/                  # 测试单元

venv/                   # 虚拟环境

dev_doc/             #开发文档

requirements.txt        # 依赖包的列表

run.py               # 项目启动控制文件

4.      常用功能

4.1.    Flask对象声明及运行

file:app.py

from flask import Flask
app = Flask(__name__)

file:run.py

from website.app.app import app

if __name__ == '__main__':
    app.run()

4.2.   
调试模式

每次修改代码后需要重启服务器来使其生效,可以打开调试模式,服务器会在代码修改后自动重新载入,并在发生错误时提供更好的支持。

有两种方法打开调试模式:

app.debug = True

app.run()

或者

app.run(debug=True)

两者没什么不同

4.3.   
路由

@app.route(‘/’)

关键词:变量,URL

4.4.   
变量

在请求时需要传入参数,

@app.route('/user/<username>')

def show_user_profile(username):

#
show the user profile for that user

return 'User %s' % username

@app.route('/post/<int:post_id>')

def show_post(post_id):

#
show the post with the given id, the id is an integer

return 'Post %d' % post_id

更详细内容见:笔记-flask-传参及返回格式化数据

4.5.   
唯一URL /重定向行为

以下两条规则的不同之处在于是否使用尾部的斜杠。:

@app.route('/projects/')

def projects():

return 'The project page'

@app.route('/about')

def about():

return 'The about page'

projects的URL是中规中举的,尾部有一个斜杠,看起来就如同一个文件夹。访问一个没有斜杠结尾的URL时Flask会自动进行重定向,帮你在尾部加上一个斜杠。

about的URL没有尾部斜杠,因此其行为表现与一个文件类似。如果访问这个URL时添加了尾部斜杠就会得到一个404错误。这样可以保持URL唯一,并帮助搜索引擎避免重复索引同一页面。

4.6.   
构造url--url_for()

url_for()用于构建指定URL,它把函数名称作为第一个参数。

推荐使用url_for(),理由如下:

  1. 构造url比直接写可读性好;
  2. 自动生成,在修改时不用到处去修改相关代码;
  3. 自动生成的路径通常是绝对路径,可以避免一些问题;

from flask import Flask, url_for

app = Flask(__name__)

@app.route('/')

def index():

return 'index'

@app.route('/login')

def login():

return 'login'

@app.route('/user/<username>')

def profile(username):

return '{}\'s
profile'.format(username)

with app.test_request_context():

print(url_for('index'))

print(url_for('login'))

print(url_for('login', next='/'))

print(url_for('profile', username='John Doe'))

/

/login

/login?next=/

/user/John%20Doe

4.7.   
HTTP 方法

HTTP (与 Web 应用会话的协议)有许多不同的访问 URL 方法。默认情况下,路由只回应 GET 请求,但是通过 route() 装饰器传递 methods 参数可以改变这个行为。这里有一些例子:

@app.route('/login', methods=['GET',
'POST'])

def login():

if request.method == 'POST':

do_the_login()

else:

show_the_login_form()

4.8.   
静态文件

动态 web 应用也会需要静态文件,通常是 CSS 和 JavaScript 文件。理想状况下,
你已经配置好 Web 服务器来提供静态文件,但是在开发中,Flask 也可以做到。
只要在你的包中或是模块的所在目录中创建一个名为 static 的文件夹,在应用中使用 /static 即可访问。

给静态文件生成 URL ,使用特殊的 'static' 端点名:

url_for('static', filename='style.css')

这个文件应该存储在文件系统上的 static/style.css 。

4.9.   
模板渲染

用Python生成HTML十分无趣,而且相当繁琐,因为你必须手动对 HTML 做转义来保证应用的安全。为此,Flask 配备了 Jinja2 模板引擎。

可以使用 render_template() 方法来渲染模板。需要做的就是将模板名和传入模板的变量。

渲染模板的简例:

from flask import render_template

@app.route('/hello/')

@app.route('/hello/<name>')

def hello(name=None):

return render_template('hello.html', name=name)

默认情况下Flask 会在 templates 文件夹里寻找模板。如果应用是个模板文件,这个文件夹应该与模板同级;如果它是一个包,那么这个文件夹作为包的子目录:

情况 1: 模块:

4.10.       
重定向和错误

你可以用 redirect() 函数把用户重定向到其它地方。放弃请求并返回错误代码,用 abort() 函数。这里是一个它们如何使用的例子:

from flask import abort, redirect, url_for

@app.route('/')

def index():

return redirect(url_for('login'))

@app.route('/login')

def login():

abort(401)

this_is_never_executed()

默认情况下,错误代码会显示一个黑白的错误页面。如果你要定制错误页面,
可以使用 errorhandler() 装饰器:

from flask import render_template

@app.errorhandler(404)

def page_not_found(error):

return render_template('page_not_found.html'), 404

5.     
response

a
view function 的返回值会被自动 converted into a response object for you.

转换规则如下:

  1. If a response object of the
    correct type is returned it’s directly returned from the view.

如果格式正确,直接返回;

  1. If it’s a string, a response
    object is created with that data and the default parameters.

如果是字符串,创建一个response obj;

  1. If a tuple is returned the
    items in the tuple can provide extra information. Such tuples have to be in the
    form (response,status, headers) or (response, headers) where
    at least one item has to be in the tuple. The status value will
    override the status code and headers can be a list or dictionary of
    additional header values.

如果返回的是一个元组,且无级中的元素遵守一定格式 (response,status, headers) or (response, headers) ,会按照元组提供的信息组成响应报文;

  1. If none of that works, Flask
    will assume the return value is a valid WSGI application and convert that into
    a response object.

If you want to get hold of the resulting
response object inside the view you can use the make_response() function.

视图函数中手动生成响应报文案例:

@app.errorhandler(404)

def not_found(error):

return render_template('error.html'), 404

You just need to wrap the return expression
with make_response() and
get the response object to modify it, then return it:

@app.errorhandler(404)

def not_found(error):

resp = make_response(render_template('error.html'), 404)

resp.headers['X-Something'] = 'A value'

return resp

6.     
sessions

除请求对象之外,还有一个 session 对象。如果对cookie有一定了解就很好理解,它是在 Cookies 的基础上实现的,并且对 Cookies 进行密钥签名。这意味着用户可以查看你 Cookie 的内容,但却不能修改它,除非用户知道签名的密钥。

要使用会话,你需要设置一个密钥。这里介绍会话如何工作:

from flask import Flask, session, redirect, url_for, escape, request

app = Flask(__name__)

@app.route('/')

def index():

if 'username' in session:

return 'Logged in as %s' % escape(session['username'])

return 'You are not logged in'

@app.route('/login', methods=['GET', 'POST'])

def login():

if request.method == 'POST':

session['username'] = request.form['username']

return redirect(url_for('index'))

return '''

<form action="" method="post">

<p><input type=text name=username>

<p><input type=submit
value=Login>

</form>

'''

@app.route('/logout')

def logout():

#
remove the username from the session if it's there

session.pop('username', None)

return redirect(url_for('index'))

# set the secret key.  keep this really secret:

app.secret_key = 'A0Zr98j/3yX
R~XHH!jmN]LWX/,?RT'

这里提到的 escape() 可以在你模板引擎外做转义(如同本例)。

7.     
message flashing

flask.flash(message, category='message')

Flashes a message to the next request. In
order to remove the flashed message from the session and to display it to the
user, the template has to call get_flashed_messages().

代码实现:

@app.route('/')

def index():

return render_template('index.html')

@app.route('/login', methods=['GET', 'POST'])

def login():

error = None

if request.method == 'POST':

if request.form['username'] != 'admin' or \

request.form['password'] != 'secret':

error = 'Invalid credentials'

else:

flash('You were
successfully logged in')

return redirect(url_for('index'))

return render_template('login.html', error=error)

8.     
总结

上述章节仅为flask基本使用方法,可以用于对flask有一个基本的了解或是写一个简单的静态网站,还需要更多的学习;

简单flask web实现过程:

    1. 工程框架,配置文件,初始运行文件
    2. 创建flask类app
    3. 书写视图函数,包括路由和传参,请求类型
    4. 模板书写和渲染。

笔记-flask基础操作的更多相关文章

  1. C#基础随手笔记之基础操作优化

    对数据的查询,删除等基本操作是任何编程语言都会涉及到的基础,因此,研究了一下C#中比较常用的数据操作类型,并顺手做个笔记. List查询时,若是处理比较大的数据则使用HashSet<T>类 ...

  2. [SQL] SQL学习笔记之基础操作

    1 SQL介绍 SQL 是用于访问和处理数据库的标准的计算机语言.关于SQL的具体介绍,我们通过回答如下三个问题来进行. SQL 是什么? SQL,指结构化查询语言,全称是 Structured Qu ...

  3. [笔记]NumPy基础操作

    学机器学习做点小笔记,都是Python的NumPy库的基本小操作,图书馆借的书看到的,怕自己还了书后忘了,就记下来. 一般习惯导入numpy时使用 import numpy as np ,不要直接im ...

  4. 笔记-mysql-管理及基础操作

    笔记-mysql使用-管理及基础操作 1.      简介 mysql是一个免费的关系型数据库,不过好像被oracle收购了.... 希望它继续免费. 1.1.    相关术语 数据库,表,列,行,冗 ...

  5. disruptor笔记之三:环形队列的基础操作(不用Disruptor类)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. Django笔记&教程 6-2 表单(Form)基础操作

    Django 自学笔记兼学习教程第6章第2节--表单(Form)基础操作 点击查看教程总目录 1 - 编写表单类 创建新的表单类的代码,一般写到一个专门的forms.py文件中(一般放在对应的app文 ...

  7. [笔记]SciPy、Matplotlib基础操作

    NumPy.SciPy.Matplotlib,Python下机器学习三大利器.上一篇讲了NumPy基础操作,这节讲讲SciPy和Matplotlib.目前接触到的东西不多,以后再遇到些比较常用的再更新 ...

  8. GitHub笔记(一)——本地库基础操作

    零.基础概念理解——可以访问廖雪峰老师的网站https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c01 ...

  9. 【代码笔记】Java常识性基础补充(二)——数组、ArrayList集合、ASCII编码、 eclipse基础操作、eclipse调试、eclipse项目导入、eclipse快捷键

    1.0 如何定义数组以及访问数组中的元素,如下所示.ArrayDemo01.java public class ArrayDemo01 { public static void main(String ...

随机推荐

  1. CSS基础必备盒模型及清除浮动

    盒模型 盒模型是有两种标准的,一个是标准模型,一个是IE模型. css如何设置两种模型 这里用到了CSS3 的属性 box-sizing /* 标准模型 */ box-sizing:content-b ...

  2. Android基础Activity篇——Intent向下一个活动传递数据

    1.向下一个活动传递数据 String data ="bilibilbilbilbili"; Intent intent1=new Intent(this,secondActivi ...

  3. TeamViewer安装使用

    1.下载安装包 官网下载最新安装包 2.安装步骤 3.连接 输入密码即可控制伙伴电脑.

  4. VS2015卸载方法

    VS2015卸载 直接再控制面板的卸载程序中找到 VS2015 的程序,邮件更改,安装程序会被打开,里面有三个选项包含卸载,点击卸载[记得在卸载前如果有打开过 VS 最好重启一下,重启后不要打开 VS ...

  5. 关于VisualStudio2010发布项目问题

    VisualStudio2010速度还是很给力的,VS2015打开机器就双100%了:VS2010机器上跑起来还是很好用的. 今天编译一个MVC3.0项目,发布时候出现诡异现象:Content文件夹里 ...

  6. JavaScript基础:比较运算符——==与 ===;!=与!==

    var x=10, y="10", m=15 x==y;//返回true x===y;//返回false x!=y;//返回false x!==y;//返回true//同理cons ...

  7. httpclient使用head添加cookie

    最近在使用接口时候,我使用get请求时,需要携带登录态,所以在get请求的时候我需要在head里面把cookie给加上,添加方式get和post完全不一样 Post方式添加cookie httpPos ...

  8. Pascal之Hello World

    Pascal入门篇. 平台:Windows 7 ultimate x64 工具:Free Pascal 下载安装,界面如下: 右键属性,选择“437(OEM-美国)”,重新打开程序,乱码消失.     ...

  9. 剑指offer28 字符串的排列

    1.全局变量可以在最后去定义并初始化,不一定非要在开头 2.此题有一种特殊情况需要考虑,比如字符串是“aa”,那输出应该是“aa”,而不是“aa,aa”,即相同的不输出.实现这个处理用了c++中的容器 ...

  10. 用js给元素加css

    1.如果是没有CSS文件,或者要修改的不在CSS文件里,那么: document.getElementById('DIV标签的ID').style.属性='属性值'; 这样就可以了.2.如果,样式是写 ...