博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
flask快速入门
阅读量:3940 次
发布时间:2019-05-23

本文共 2128 字,大约阅读时间需要 7 分钟。

Flask学习笔记

app = Flask(name)

app.config 可以定义路由和路由参数

app是应用程序实例.

路由返回字符串或者模版

  • 返回字符串:
    returen ‘hello world’
  • 返回模版 :
    前提: from flask import render_templates
    return render_template(‘index.html’)

定义get/put/POST等其他请求:

默认 get

@app.route(’/’,methods=[‘GET’,‘POST’])//这样就可以使用post访问到了

给路由传参:

app.route(’/orders/<order_id>’)

def getOrderById(order_id): # 在函数的参数也写上 路由参数
return ‘Order id %s’ % order_id

给路由参数限定格式:

app.route(’/orders/int:order_id’) or

app.route(’/orders/float:order_id’)

jinja2 模版引擎:

  • 写网页,然后把数据填充到网页里面
  • 先返回一个网页(模版):

如何给模版填充数据:

@app.route('/',methods=['GET','POST'])def hello_world():    # 返回模版内容;    urlString = 'www.baidu.com'    return render_template('index.html',templateName=urlString) // 第一个是模版名字,后面是传入参数的键值对.    templateName(在模板中使用). urlString(传入的变量名.)在html文件中使用{
{urlString}}使用传入的变量.

{

{}} // 变量代码块

变量代码块的使用:

  • 注释: {# 这是一个注释 #}

    一般模版里面的变量名和要传入的变量名保持 一致

  • 数组的使用:

myList = [1,2,3,4,5]

{
{ myList}}
{
{ myList.2 }}
{
{ myList[2]}}

  • 字典的使用:

    myDict = {

    ‘name’:‘zhoufu’,
    ‘age’:‘0304170106’,
    ‘gender’:‘man’
    }
    {
    { myDict }}
    {
    { myDict[‘name’] }}
    {
    { myDict.name }}

控制代码块的使用:

{% %}

  • for循环: if-else控制:

{% for i in myList %} {# //基本for 循环 for然后按住tab #}

{% if i > 3 %}
{
{ i }}
{% endif %}
{% endfor %}

过滤器: (模版里面的函数.)

使用方法: 变量名|过滤器.

字符串操作

{# 参数 | 函数名 #}
{
{ templateName | upper }}

列表操作

{
{myList | first }}
// 排序.求和,长度

链式调用

其实就是多个函数,按照顺序起作用.
{
{ templateName | upper |reverse }}

Web表单:

实现简单登陆的逻辑处理:

判断请求的方式.获取表单数据:

from flask import request#获取到后端:    if request.method =='POST':        # 获取表单的数据,请求参数:        username = request.form.get('username')        password = request.form.get('password')        print(username)        if not all ([username,password]):            print('数据参数不完整')        else:            return 'success'
  • 补充:
    flash(‘ ’ ) //需要设置 secret_key: app.secret_key = ‘zhoufu’

flash(u’数据参数不完整’)

{% for message in get_flashed_messages() %}
{
{ message }}
{% endfor %}

Flask-WTF实现表单 暂时不学了(现在推荐前后端分离实现:)

Flask-SQLALchemy 扩展

from sqlalchemy import Column, String, create_engine, ForeignKeyfrom sqlalchemy.orm import sessionmaker, relationshipfrom sqlalchemy.ext.declarative import declarative_base

导入的包,都是新的.

数据库基本操作

之后的文章里面仔细有讲解

转载地址:http://pcywi.baihongyu.com/

你可能感兴趣的文章
Python 正则表达式(基础)
查看>>
Python 正则表达式(常用函数)
查看>>
Python 正则表达式(分组)
查看>>
python 文本解析 XML基础
查看>>
Python XML的解析与创建
查看>>
Python 创建XML
查看>>
Python JSON 解析
查看>>
Python Excel解析
查看>>
Python 多进程 fork()详解
查看>>
Python 多进程 multiprocessing.Pool类详解
查看>>
Python 多线程 threading和multiprocessing模块
查看>>
Python 线程同步与互斥
查看>>
Codeforces Round #400 (Div. 1 + Div. 2, combined)D - The Door Problem(2-sat)
查看>>
IDEA中Struts2文件上传时404错误The origin server did not find a current representation for the target resour
查看>>
Perl/Tk 变量追踪及类线程实现
查看>>
1.嵌入式开发环境搭建--虚拟机安装(unbutu)系统
查看>>
2.嵌入式开发环境搭建--(unbutu)系统
查看>>
Linux USB驱动分析之USB2.0协议分析
查看>>
关于iwpriv :no private ioctls 的问题
查看>>
GPIO ,ioctl,file->private_data
查看>>