flask

一、flask结构

1、新建项目

pytharm社区版新建,构建文件夹,及app.py文件
在这里插入图片描述
templates 存放html页面
static 存放图片,配置等

2、配置app.py

from flask import Flask  ,render_template, request #引用包,flask框架,路由,页面

app=Flask(__name__) #初始化

@app.route("/") #url路径
def hell():  #执行内容
    return  "hell world"

if __name__=='__main__':  #项目启始
    app.run(debug=True)		#启用调试

3、访问页面

 http://127.0.0.1:5000/

二、配置类型

1、基本样式

#返回文本
@app.route('/')
def index():
    name='23333'
    return '<h1>Hello 11World</h1>'+name

@app.route('/index')
def index1():
    return 'xxxxxx'

#获取url参数,字符型
@app.route('/user/<name>')  
def wecom(name):
    return "你好,%s"%name
    
#获取url参数,整型,支持浮点型float
@app.route('/user/<int:id>')
def wecom2(id):
    return "你好,%d 会员号"%id

#调用页面,页面文件默认在templates目录下
@app.route("/a")
def index2():
    return render_template("index.html")

2、传参

接口参数传递给html

#传递参数
@app.route("/b")
def index3():
    time=datetime.date.today()
    name=["a","b","c"]
    task={"任务":"动作1","时间":time}
    return render_template("index.html",var=time,list=name,task=task)

html接收参数

<--接收值 var为名称-->
{{ var}}

<-- 接收list -->
{% for lis in list %}
	 <li> {{lis}} </li>
{% endfor %}

<-- 接收map\json -->
<table border="1">
    {% for key,value in task.items() %}
    <tr>
        <td>{{key}}</td>
        <td>{{value}}</td>
    </tr>
    {%endfor%}

</table>

3、表单提交

路由

@app.route('/test/register')
def frominfo():
    return render_template("test/register.html")

@app.route('/result1',methods=['post','get']) #访问类型,post获取表单内容
def result():
    if request.method=='POST':
        result=request.form #获取表单内容
    return render_template("test/result.html",result=result)

表单填写

<form action="{{url_for('result')}}" method="post">
    <p>姓名:<input type="text" name="name"></p>
    <p>年龄:<input type="text" name="age"></p>
    <p>性别:<input type="text" name="sex"></p>
    <p>地址:<input type="text" name="address"></p>
    <p><input type="submit" value="提交"></p>
</form>

接收表单,展示表单

<table border="1">
    {% for key,value in result.items() %}
    <tr>
        <td>{{key}}</td>
        <td>{{value}}</td>
    </tr>
    {% endfor %}
</table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值