本文共 2128 字,大约阅读时间需要 7 分钟。
app = Flask(name)
app.config 可以定义路由和路由参数
app是应用程序实例.
默认 get
@app.route(’/’,methods=[‘GET’,‘POST’])//这样就可以使用post访问到了app.route(’/orders/<order_id>’)
def getOrderById(order_id): # 在函数的参数也写上 路由参数 return ‘Order id %s’ % order_idapp.route(’/orders/int:order_id’) or
app.route(’/orders/float:order_id’)如何给模版填充数据:
@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 i in myList %} {# //基本for 循环 for然后按住tab #}
{% if i > 3 %} { { i }} {% endif %} {% endfor %}使用方法: 变量名|过滤器.
字符串操作
{# 参数 | 函数名 #} { { templateName | upper }}列表操作
{ {myList | first }} // 排序.求和,长度链式调用
其实就是多个函数,按照顺序起作用. { { templateName | upper |reverse }}实现简单登陆的逻辑处理:
判断请求的方式.获取表单数据:
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(u’数据参数不完整’)
{% for message in get_flashed_messages() %} { { message }} {% endfor %}from sqlalchemy import Column, String, create_engine, ForeignKeyfrom sqlalchemy.orm import sessionmaker, relationshipfrom sqlalchemy.ext.declarative import declarative_base
导入的包,都是新的.
之后的文章里面仔细有讲解
转载地址:http://pcywi.baihongyu.com/