django部分 | html部分 | 笔记心得

115 阅读2分钟

django部分

html部分

web.py

from flask import Flask,render_template

app=Flask(__name__)

@app.route("/show/info")
def index():
    # return "yasuo"
    # 默认是去当前目录的templates 模板
    return render_template("index.html")

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

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>亚索!</h1>
    <div>
        <span style="color: red;">时间: </span>
        <span >2021-12-12</span>
    </div>
    <div>
        亚索是一个百折不屈的艾欧尼亚人,也是一名身手敏捷的御风剑客。这位生性自负的年轻人,被误认作杀害长老的凶手——无法自证清白的亚索因此出于自卫而出手杀死了自己的哥哥。之后,虽然长老死亡的真相已然大白,但亚索并无法原谅自己的所作所为。他将自己放逐于天涯,听任疾风指引着剑刃的方向。
    </div>
    <h2>断剑的自白:第一幕</h2>
    <div>
        <span style="color: red;">作者:<span>
        <span>ARIEL LAWRENCE</span>
    </div>
    <div>诺克萨斯和艾欧尼亚战后余烬未灭,锐雯正面临一条滔天大罪的指控。
    </div>
</body>
</html>

href跳转

<a href="/get/news">看新鲜事!</a>
<a href="https://yz.lol.qq.com/zh_CN/champion/yasuo/">亚索的路!</a>

img添加

<img src="https://game.gtimg.cn/images/lol/universe/v1/assets/blt41c476486b063ef8-Yasuo_0.jpg" alt="">
<!-- 需要创建static目录存储本地文件 -->
<img style="height: 10%;width: 10%;" src="/static/02.jpg">

嵌套

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ikun</title>
</head>
<body>
    <a href="https://zhuanlan.zhihu.com/p/560821660" target="_blank">
        <img src="/static/ikun/01.jpg" alt="" style="width: 100px;">
    </a>
    <a href="https://zhuanlan.zhihu.com/p/560821660">
        <img src="/static/ikun/02.jpg" alt="" style="width: 100px;">
    </a>
    <a href="https://zhuanlan.zhihu.com/p/560821660">
        <img src="/static/ikun/03.jpg" alt="" style="width: 100px;">
    </a>
</body>
</html>

表格三件套

<div>
        <ul>
            <li>yasuo</li>
            <li>yongen</li>
        </ul>

        <ol>
            <li>有序号的</li>
            <li>列表</li>
        </ol>
    </div>
    <table border="1">
        <thead>
            <tr>
                <th>id</th>
                <th>age</th>
                <th>gender</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>10</td>
                <td>12</td>
                <td>男人</td>
            </tr>
            <tr>
                <td>10</td>
                <td>10</td>
                <td>男人</td>
            </tr>
        </tbody>
    </table>

交互部分

<div>
        <!-- 文本密码文件 -->
        <input type="text">
        <input type="password">
        <input type="file">
        <!-- 进行互斥选择 -->
        <input type="radio"name="n1">男人
        <input type="radio" name="n1">女人

        <input type="checkbox">1
        <input type="checkbox">2
        <input type="checkbox">3
        
        <!-- 普通按钮 -->
        <input type="button" value="提交">
        <!-- 提交表单 -->
        <input type="submit" value="提交!">
        <!-- 多行文本 -->
        <textarea name="" id="" cols="30" rows="10"></textarea>
    </div>

    <div>
        <!-- 下拉框 multiple进行多选!-->
        <select name="" id="" multiple>
            <option value="">1</option>
            <option value="">2</option>
            <option value="">3</option>
        </select>
    </div>

简单demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>用户注册</h1>
    <div>
        用户名:<input type="text" />
    </div>
    <div>
        密码:<input type="password">
    </div>
    <div>
        性别:<input type="radio" name="gender"><input type="radio" name="gender"></div>
    <div>
        爱好:
            <input type="checkbox"><input type="checkbox"><input type="checkbox">rap
    <div>
        城市:
        <select name="" id="">
            <option value="">北京</option>
            <option value="">西安</option>
            <option value="">上海</option>
        </select>
    </div>
    <div>
        擅长领域: 多选需要按ctrl
        <select name="" id="" multiple>
            <option value="">游戏</option>
            <option value="">谈恋爱</option>
            <option value="">做ikun</option>
        </select>
    </div>
    <div>
        备注:
        <textarea name="" id="" cols="10" rows="3"></textarea>
    </div>
    <div>
        <input type="button" value="button提交">
        <input type="submit" value="submit提交">
    </div>
    </div>
</body>
</html>

数据提交

<h1>用户注册</h1>
    <!-- form标签包裹数据 action是提交的地址 name method -->
    <form action="/yasuo/" method="get">
        用户名:<input type="text" name="id"/>
        密码:<input type="password" name="password">
        <input type="submit" value="submit提交">
    </form>

拿到用户提交的数据

from flask import Flask, render_template,request
# request接受用户的请求!
app = Flask(__name__)


@app.route("/register", methods=["GET"])
def register():
    return render_template("register.html")


@app.route("/do/reg", methods=["GET"])
def do_register():
    print(request.args)
    return "注册成功"


@app.route("/post/reg", methods=["POST"])
def post_register():
    print(request.form)
    return "注册成功"


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

小总结

app.py

from flask import Flask, render_template,request

app = Flask(__name__)

@app.route("/register", methods=["GET","POST"])
def register():
    # 
    if request.method=="GET":
        return render_template("register.html")
    else:
        for k,v in request.form.items():
            print(k,v)
        return "注册成功"

@app.route("/login", methods=["POST","GET"])
def login():
    if request.method=="POST":
        print(request.form)
        return "登录成果!"
    else:
        return render_template("login.html")
@app.route("/index", methods=["GET"])
def index():
    return render_template("index.html")
if __name__ == '__main__':
    app.run()