欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python flask库编写网页入门

程序员文章站 2024-02-15 15:11:23
...

简单的flask程序

from flask import Flask

app = Flask(__name__)


@app.route('/')    #路由解析,通过用户访问的路径,匹配对应的函数
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(debug = True) #开启刷新页面更新内容,不用一直重开服务器


python flask库编写网页入门
python flask库编写网页入门

简单的应用html文件渲染

python代码:

from flask import Flask,render_template        #render_template为渲染模板库
import datetime

app = Flask(__name__)


@app.route('/index')    #路由解析,通过用户访问的路径,匹配对应的函数
def hello():
    return  "hello world!"

#通过访问路径,获取用户的字符串参数
@app.route('/user/<name>')
def welcome(name):
    return "你好,%s号会员"%name   #用<>拿到网页地址的内容然后传给welcome函数

#通过访问路径,获取用户的整型参数,此外还有float类型
@app.route('/user/<int:id>')
def welcome2(id):
    return "你好,%d"%id   #用<>拿到网页地址的内容然后传给welcome函数

#路由路径不能重复,用户只能通过唯一路径访问特定函数

#返回给用户渲染后的网页文件
#@app.route('/')
#def index2():
#    return render_template("index.html")  #将index.html文件渲染到网页,index.html文件要放templates中

#向页面传递变量
@app.route('/')
def index2():
    time = datetime.date.today()     #普通变量
    name = ["小张","小王","小赵"]     #列表类型
    task = {"任务":"打扫卫生","时间":"3小时"}     #字典类型
    return render_template("index2.html",var = time,list = name,task =task)  #通过参数var向页面传递参数

if __name__ == '__main__':
    app.run(debug = True) #开启刷新更新内容,不用一直重开服务器

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
      今天是{{ var }},欢迎光临。<br/>
      今天值班的有:<br/>
      {%  for data in list %}              <!--用大括号和百分号括起来的是控制结构,还有if-->
          <li> {{ data }}</li>            <!--以列表形式显示-->
      {% endfor %}                         <!--for循环的结束-->

       任务:<br/>                       <!--打印表格,迭代-->
            <table border = "1">
                {% for key,value in task.items() %}   <!-[(key,value),(key,values),(key,values)]-->
                    <tr>
                    <td>{{ key }}</td>
                    <td>{{ value }}</td>
                    </tr>
                {% endfor %}
                <table>
</body>
</html>

效果图:
python flask库编写网页入门

表单提交:

python代码:

from flask import Flask,render_template,request        #render_template为渲染模板库,request用于获取页面填写内容
import datetime

app = Flask(__name__)
#表单提交
@app.route('/test/register')
def register():
        return render_template("register.html")

#接收表单提交的路由,需要指定method为post
@app.route('/result',methods=['POST','GET'])
def result():
    if request.method == 'POST':  #如果是被其他页面请求进入
        result = request.form  # 获取的输入内容为字典
        return render_template("result.html",result=result)

if __name__ == '__main__':
    app.run(debug = True) #开启刷新更新内容,不用一直重开服务器

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

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

</body>
</html>

python flask库编写网页入门

result.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
       <table border = "1">
                {% for key,value in result.items() %}   <!-[(key,value),(key,values),(key,values)]-->
                    <tr>
                         <th>{{ key }}</th>              <!-标题栏th-->
                         <td>{{ value }}</td>
                    </tr>
                {% endfor %}
                <table>
</body>
</html>

python flask库编写网页入门

相关标签: python 网页设计