1. 程式人生 > >學習日記 | 5.29 [Python3] Python Web開發基礎

學習日記 | 5.29 [Python3] Python Web開發基礎

odi 編碼 AC htm extends app render www amp

註:這是一系列基於實驗樓網絡培訓的python學習日記,內容零散,只是便於我自己回顧,有需要請了解www.shiyanlou.com。


3. 實驗13: jinja2模板

flask的默認模板通過jinja2實現。

jinja使用一些特殊字符包含需要被解釋執行的代碼:{%語法語句%} {{python對象}} {#註釋#}。

通過set給jinja賦值變量:

{% set result = heavy_operation() %}

<p> {{ result }}</p>

在jinja2中用macro定義宏,也支持導入宏,如下:

{% from ‘macro.html‘ import course_item %}

<div> {{ course_item(course) }} </div>

模板繼承:方便編碼共用的標題欄和尾部,繼承功能通過block,extends等關鍵字實現。

示例:

宏 macro.html:

{% macro course_item(course, type="bootstrap") %}
    <div>
        <p> type: {{ type }}</p>
        <p> name: {{course.name }}</p>
        <p> is_private: {{ course.is_private }}</
p> </div> {% endmacro %}

須繼承的 base.html:

<body>
    <div>
        {% block header %}
            <p> this is header </p>
        {% endblock %}
    </div>
    <div>{% block content %}{% endblock %}</div>
    <div id="footer">
        {% block footer %}
        
&copy; Copyright2017 by <a href="http://www.shiyanlou.com/">shiyanlou</a>. {% endblock %} </div> </body>

index.html:

<h1> this is the header</h1>

<p> name: {{ course.name }}</p>
<p> user count: {{ course.user_count }}</p>
<p> teacher: {{ course.teacher }}</p>
<p> teacher email: {{ course.teacher.email | hidden_email }}</p>
<p> course tag length: {{ course.tags | length }}</p>
<p> is_private: {{ course.is_private }}</p>
<p> not exist: {{ course.not_exist }}</p>

{% if course.is_private %}
    <p> course {{ course.name }} is private</p>
{% elif course.is_member_course %}
    <p> course {{ course.name }} is member course </p>
{% else %}
    <p> course {{ course.name }} is normal course </p>
{% endif %}

{% for tag in course.tags %}
    <span> {{ tag }} </span>
{% endfor %}

{% macro course_item(course, type="bootstrap") %}
    <div>
        <p> type: {{ type }}</p>
        <p> name: {{ course.name }}</p>
        <p> user count: {{ course.user_count }}</p>
        <p> teacher: {{ course.teacher }}</p>
        <p> is_private: {{ course.is_private }}</p>
    </div>
{% endmacro %}

<div> {{ course_item(course) }}</div>
<p> {{ "=" * 20}}</p>
<div> {{ course_item(course, type="louplus") }}</div>

app.py:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from flask import Flask, render_template

app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True

def hidden_email(email):
    parts = email.split(@)
    parts[0] = "*****"
    return @.join(parts)

app.add_template_filter(hidden_email)

@app.route(/)
def index():
    teacher = {
            "name": "tom",
            "email": "[email protected]"
    }
    course = {
            "name": "Python Basic",
            "teacher": teacher,
            "user_count": 5348,
            "price": 199.0,
            "lab": None,
            "is_private": False,
            "is_member_course": True,
            "tags": [python, big data, linux]
    }
    return render_template("index.html", course=course)

學習日記 | 5.29 [Python3] Python Web開發基礎