码迷,mamicode.com
首页 > 其他好文 > 详细

Flask基础1

时间:2018-12-20 20:30:09      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:基础   user   inf   isl   cal   ati   secret   ue4   语言   

from flask import Flask,render_template,redirect,jsonify,send_file,request,Markup,session
app = Flask(__name__)

app.secret_key = "yinjiaodawangba"

STUDENT = {name: Old, age: 38, gender: }

STUDENT_LIST = [
    {name: Old, age: 38, gender: },
    {name: Boy, age: 73, gender: },
    {name: EDU, age: 84, gender: }
]

STUDENT_DICT = {
    1: {name: Old, age: 38, gender: },
    2: {name: Boy, age: 73, gender: },
    3: {name: EDU, age: 84, gender: }
}

@app.template_global()
def ab(a,b):
    return a+b

@app.template_filter()
def axb(a,b):
    return a*b

@app.route("/")
def index():
    inp = Markup("<input type=‘text‘>")
    if session.get("user"):
        print(session)
        # return json.dumps({"name":"JWB","age":73})
        # return jsonify({"name":"JWB","age":73})
        return render_template("index.html",stu=STUDENT_DICT,inp=inp)
    
    return redirect("/login")


@app.route("/bl")
def bl():
    # return json.dumps({"name":"JWB","age":73})
    # return jsonify({"name":"JWB","age":73}
    
    return render_template("my_block.html", stu=STUDENT_DICT)


@app.route("/login",methods=("GET","POST"))
def login():
    if request.method == "GET":
        return render_template("login.html")
    if request.method == "POST":
        user_info = request.form.to_dict()
        if user_info.get("username") == "JWB" and user_info.get("pwd") == "DSB":
            session["user"] = user_info.get("username")
            return redirect("/")
        else:
            return render_template("login.html",msg="用户名密码错误")
        
    

if __name__ == __main__:
    app.run(host="0.0.0.0",port=9527,debug=True)


# eyJ1c2VyIjoiSldCIn0.DvS9eg.aXNffcXrztlPEzfR8ulOlU3Xbww
# .eJyrViotTi1SslLyCndS0gFzDFF4Rig8YxSeCQrPFMqrBQDoaBcs.DvS9vQ.sW0iFbfMJmAz8kOZJ36aWRclY0c
# .eJxFTTsSAiEMvYpDbUFCwsfS0gvY0MDCHkCHyvHuJsjONm9e8n4fM979ZW7m8byb6zxArjwYKAp2aIIINQ_qUE4X_jP6RrEky24p7lB8wCJhR2UpdGY2VRIm5XaW26RD1i8vH97gp7e1LpwrCt9tuUh99ZRHrG2OBJCPRRZeOWphAUWS8pC2OcG7-f4AmopIXQ.DvS-LA.lrKb_t7s7TVnDlwICSYEANSonJ8

 

jinja2和模板语言的用法一样

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>学生信息</title>
</head>
<body>
{% include "my_block.html" %}
    {{ stu }}
<table border="2px">
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <th>gender</th>
        </tr>
    </thead>
    <tbody>
    {% for id,student in stu.items() %}
        <tr>
            <td>{{ id }}</td>
            <td>{{ stu[id].name }}</td>
            <td>{{ stu.get(id)["age"] }}</td>
            {% if stu[id]["gender"] != "" and stu[id]["gender"] != "" %}
                <td>女</td>
            {% else %}
                <td>{{ stu[id]["gender"] }}</td>
            {% endif %}

        </tr>
    {% endfor %}

    </tbody>
</table>
{{ inp}}
{{ ab(6,9) }}
{{ ab(3,4) | axb(834473503) }}

{% macro my_input(na,tp) %}
    <input type="{{ tp }}" value="{{ na }}">
{% endmacro %}

{{ my_input("uname","submit") }}

{% block content %}

{% endblock %}

</body>
</html>

 

Flask基础1

标签:基础   user   inf   isl   cal   ati   secret   ue4   语言   

原文地址:https://www.cnblogs.com/shuai1993/p/10151678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!