使用flask框架搭建web項目的簡單操作

使用這些基本操作可以節省大量重複代碼,提高了代碼複用性,使項目更加高內聚,低耦合.

1.靜態資源定位(防止在文件夾層級複雜的情況下,定位資源文件出錯導致顯示不了效果)

//home目錄下的comments.html文件
<a href="{{ url_for('home.comments') }}"></a>

<script src="{{ url_for('static',filename='base/js/wow.min.js') }}"></script>

<link rel="stylesheet" href="{{ url_for('static',filename='base/css/animate.css') }}">

2.路由設置

from flask import render_template, redirect, url_for

//跳轉
@home.route("/login/")
def login():
    return render_template("home/login.html")

//重定向
@home.route("/logout/")
def logout():
    return redirect(url_for("home.login"))

3.數據塊

//繼承
{% extends "home/home.html" %}

 {% block css %}
        <style>
     
        </style>
 {% endblock %}

{% block content %}
    {% include "home/menu.html" %}
  
{% endblock %}

{% block js %}
<script>
  
</script>
{% endblock %}

4.for循環(循環重複的模板性的h5代碼)

{% for v in range(1,13) %}

 {% endfor %}

後續操作更新中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章