欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一個基于flask的web應(yīng)用誕生 bootstrap框架美化(3)

 更新時間:2017年04月11日 09:13:56   作者:雙魚座的牛  
一個基于flask的web應(yīng)用誕生第三篇,這篇文章主要介紹了前端框架bootstrap與flask框架進(jìn)行整合,具有一定的參考價值,感興趣的小伙伴們可以參考一下

經(jīng)過上一章的內(nèi)容,其實就頁面層來說已結(jié)可以很輕松的實現(xiàn)功能了,但是很明顯美觀上還有很大的欠缺,現(xiàn)在有一些很好的前端css框架,如AmazeUI,騰訊的WeUI等等,這里推薦一個和flask集成很好的bootstrap框架

安裝框架

在模板中直接引用bootstrap的CDN或者本地路徑外,還可以直接應(yīng)用flask的bootstrap集成包,首先需要對集成包進(jìn)行安裝:

pip3.6 install flask-bootstrap

這是一個flask的擴(kuò)展包,flask的所有擴(kuò)展包默認(rèn)默認(rèn)的包名都為flask.ext打頭,同樣bootstrap也是如此,首先在default的文件的頭部導(dǎo)入包:

from flask.ext.bootstrap import Bootstrap

然后對bootstrap進(jìn)行初始化,修改代碼:

bootstrap=Bootstrap(app)

初始化之后,就可以使用Jinja2的繼承方式使用此包中的包含的一系列的針對Bootstrap的基模板。基模板中直接引用了一系列的bootstrap中的元素。

還記得如何在jinja2中使用模板繼承吧,下面在使用之前,首先看看基模板的結(jié)構(gòu):

{% block doc -%}
<!DOCTYPE html>
<html{% block html_attribs %}{% endblock html_attribs %}>
{%- block html %}
 <head>
 {%- block head %}
 <title>{% block title %}{{title|default}}{% endblock title %}</title>

 {%- block metas %}
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 {%- endblock metas %}

 {%- block styles %}
 <!-- Bootstrap -->
 <link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="external nofollow" rel="stylesheet">
 {%- endblock styles %}
 {%- endblock head %}
 </head>
 <body{% block body_attribs %}{% endblock body_attribs %}>
 {% block body -%}
 {% block navbar %}
 {%- endblock navbar %}
 {% block content -%}
 {%- endblock content %}

 {% block scripts %}
 <script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script>
 <script src="{{bootstrap_find_resource('js/bootstrap.js', cdn='bootstrap')}}"></script>
 {%- endblock scripts %}
 {%- endblock body %}
 </body>
{%- endblock html %}
</html>
{% endblock doc -%}

從源碼中可以看出,這個基模板定義了12個block,分別對應(yīng)了整個文檔(doc),html屬性(html_attribs),整個html(html),整個head部分(head),title部分(title),meta代碼部分(metas),css樣式(styles),body屬性(body_attribs),body部分(body),導(dǎo)航(navbar),
頁面內(nèi)容(content),js(scripts)

并且title,meta,css,和js均有默認(rèn)的內(nèi)容,所以使用的時候需要加入{{super()}}

好,根據(jù)這個基模板的結(jié)構(gòu),修改login.html中的代碼為:

{% extends "bootstrap/base.html"%}

{% block title%}牛博客 {% endblock %}<!--覆蓋title標(biāo)簽-->

{% block navbar %}
<nav class="navbar navbar-inverse"><!-- 導(dǎo)航部分 -->
 導(dǎo)航
</nav>
{% endblock %}
{% block content %} <!--具體內(nèi)容-->
<div class="container">
 <div class="container">
 <form method="post">
 <div class="form-group">
 <label for="username">用戶名</label>
 <input type="text" class="form-control" id="username" placeholder="請輸入用戶名">
 </div>
 <div class="form-group">
 <label for="passworld">密碼</label>
 <input type="password" class="form-control" id="passworld" placeholder="請輸入密碼">
 </div>
 <button type="submit" class="btn btn-default">登錄</button>
 </form>
 </div>
</div>
{% endblock %}

運(yùn)行程序,現(xiàn)在的顯示結(jié)果為:

比剛剛漂亮多了,這時生成的html代碼為:

<!DOCTYPE html>
<html>
 <head>
 <title>牛博客 </title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <!-- Bootstrap -->
 <link  rel="external nofollow" rel="external nofollow" rel="stylesheet">
 </head>
 <body>
 <nav class="navbar navbar-inverse"><!-- 導(dǎo)航部分 -->
 導(dǎo)航
 </nav>
 <!--具體內(nèi)容-->
 <div class="container">
 <form method="post">
 <div class="form-group">
 <label for="username">用戶名</label>
 <input type="text" class="form-control" id="username" placeholder="請輸入用戶名">
 </div>
 <div class="form-group">
 <label for="passworld">密碼</label>
 <input type="password" class="form-control" id="passworld" placeholder="請輸入密碼">
 </div>
 <button type="submit" class="btn btn-default">登錄</button>
 </form>
 </div>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
 </body>
</html>

注意這幾個cdn的地址,這個地址有時候會被擋在墻外,這時怎么辦呢?

修改的方式為在python的安裝目錄下找到Lib\site-packages\flask_bootstrap文件夾,文件夾下有__init__.py文件,打開后看到如下代碼:

進(jìn)行修改,順便提一下,我比較常使用bootcdn這個cdn服務(wù)器

下面使用土法進(jìn)行一下測試,輸入test和123后的結(jié)果為:

顯示的還是之前的測試登錄成功頁,這顯然是不對的,一般來說,bbs或blog都是跳到登錄前的頁面或者首頁,現(xiàn)在為了方便起見,都跳轉(zhuǎn)到首頁,同時,如果用戶名或密碼錯誤,也要在登錄頁進(jìn)行提示,修改default.py代碼如下:

from flask import session #導(dǎo)入session對象

@app.route("/login",methods=["POST"])
def loginPost():
 username=request.form.get("username","")
 password=request.form.get("password","")
 if username=="test" and password=="123" :
 session["user"]=username
 return render_template("/index.html",name=username,site_name='myblog')
 else:
 return "登錄失敗"

登錄成功后的源碼為:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>myblog</title>
</head>
<body>
<h1>這個站點(diǎn)的名字為 myblog </h1>
</body>
</html>

哦,對了,沒有引用bootstrap的基模板,修改index.html的模板代碼,將第一行的

{% extends "base.html" %}

修改為

{% extends "bootstrap/base.html" %}

刷新為:

<!DOCTYPE html>
<html>
 <head>
 <title>blog</title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <!-- Bootstrap -->
 <link  rel="stylesheet">
 </head>
 <body>
 <h1>這個站點(diǎn)的名字為 myblog </h1>
 </body>
</html>

看到已經(jīng)成功引用了bootstrap框架,但是導(dǎo)航部分全部都沒有,這時當(dāng)然不能在寫一遍導(dǎo)航,直接修改自定義的基模板,然后讓其他模板引用即可,修改基模板為:

{%extends "bootstrap/base.html "%}
{% block title%}牛博客 {% endblock %}<!--覆蓋title標(biāo)簽-->

{% block navbar %}
<nav class="navbar navbar-inverse"><!-- 導(dǎo)航部分 -->
 導(dǎo)航
</nav>
{% endblock %}
{% block content %} <!--具體內(nèi)容-->
<div class="container">
</div>
{% endblock %}

然后修改首頁代碼:

{% extends "base.html" %}

{% block content %}
 <h1>這個站點(diǎn)的名字為 {{site_name}} </h1>
{% endblock %}

修改登錄頁代碼為:

{% extends "base.html"%}
{% block content %} <!--具體內(nèi)容-->
<div class="container">
 <form method="post">
 <div class="form-group">
 <label for="username">用戶名</label>
 <input type="text" class="form-control" name="username" id="username" placeholder="請輸入用戶名">
 </div>
 <div class="form-group">
 <label for="passworld">密碼</label>
 <input type="password" class="form-control" name="password" id="passworld" placeholder="請輸入密碼">
 </div>
 <button type="submit" class="btn btn-default">登錄</button>
 </form>
</div>
{% endblock %}

下面登錄成功頁的顯示結(jié)果為:

頁面風(fēng)格與登錄頁保持了一致,但是,目前還是如果用戶名密碼錯誤(即輸入的不是test和123),那么除了和剛剛一樣返回一個登錄錯誤的字符串之外,用戶是無法獲悉的,就需要一個反應(yīng)用戶狀態(tài)的方法,這一點(diǎn),flask提供了flash函數(shù),下面繼續(xù)修改default.py文件:

from flask import flash

@app.route("/login",methods=["POST"])
def loginPost():
 username=request.form.get("username","")
 password=request.form.get("password","")
 if username=="test" and password=="123" :
 session["user"]=username
 return render_template("/index.html",name=username,site_name='myblog')
 else:
 flash("您輸入的用戶名或密碼錯誤")
 return render_template("/login.html") #返回的仍為登錄頁

修改login.html模板:

{% extends "base.html"%}
{% block content %} <!--具體內(nèi)容-->
<div class="container">

 {% for message in get_flashed_messages() %}
 <div class="alert alert-warning">
 <button type="button" class="close" data-dismiss="alter">&times</button>
 {{message}}
 </div>
 {% endfor %}

 <form method="post">
 <div class="form-group">
 <label for="username">用戶名</label>
 <input type="text" class="form-control" name="username" id="username" placeholder="請輸入用戶名">
 </div>
 <div class="form-group">
 <label for="passworld">密碼</label>
 <input type="password" class="form-control" name="password" id="passworld" placeholder="請輸入密碼">
 </div>
 <button type="submit" class="btn btn-default">登錄</button>
 </form>
</div>
{% endblock %}

好下面輸入test和1234,顯示結(jié)果為:

狀態(tài)很完美的顯示出來。

繼續(xù)美化

登錄的頁面和控制器的基本功能都已經(jīng)完成,但是僅僅就現(xiàn)在這個頁面來說,沒有登錄框占整個屏幕的,一般來說,都是居中的一部分,這塊不涉及flask的部分,輪到bootstrap的柵格系統(tǒng)登場了。

柵格系統(tǒng)簡單說就是將一個container或container-fluid中分為12個列,每個列都可以合并或偏移,與html中的table類似,并且支持響應(yīng)式,通過xs,sm,md,lg來進(jìn)行不同屏幕尺寸的區(qū)分。下面用柵格系統(tǒng)對登錄頁進(jìn)行一下修改:

{% extends "base.html"%}
{% block content %} <!--具體內(nèi)容-->
<div class="container">
 <div class="row"></div>
 <div class="row">
 <#-- col-md-4表示合并4列,col-md-offset-4表示偏移4列 sm意思相同 --#>
 <div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3">
 <div class="page-header">
 <h1>歡迎您登陸</h1>
 </div>
 {% for message in get_flashed_messages() %}
 <div class="alert alert-warning">
 <button type="button" class="close" data-dismiss="alter">&times</button>
 {{message}}
 </div>
 {% endfor %}

 <form method="post">
 <div class="form-group">
 <label for="username">用戶名</label>
 <input type="text" class="form-control" name="username" id="username" placeholder="請輸入用戶名">
 </div>
 <div class="form-group">
 <label for="passworld">密碼</label>
 <input type="password" class="form-control" name="password" id="passworld" placeholder="請輸入密碼">
 </div>
 <button type="submit" class="btn btn-default">登錄</button>
 </form>
 </div>
 </div>
</div>
{% endblock %}


顯示結(jié)果如下:

畢竟不是專業(yè)美工,沒有經(jīng)過設(shè)計,但至少比剛剛美觀多了,但登錄的用戶名和密碼寫成固定值肯定是不行的,數(shù)據(jù)庫是必不可少的,將在下一章讓flask和mysql進(jìn)行互聯(lián)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中檢查字符串是否僅包含字母的方法詳解

    Python中檢查字符串是否僅包含字母的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Python中的多種方法來檢查字符串是否只由字母組成,以及它們的應(yīng)用場景和優(yōu)劣,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • Python內(nèi)置函數(shù)int()的具體使用

    Python內(nèi)置函數(shù)int()的具體使用

    這篇文章主要為大家介紹了Python內(nèi)置函數(shù)int()的具體使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • python輸入整條數(shù)據(jù)分割存入數(shù)組的方法

    python輸入整條數(shù)據(jù)分割存入數(shù)組的方法

    今天小編就為大家分享一篇python輸入整條數(shù)據(jù)分割存入數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python3+Django get/post請求實現(xiàn)教程詳解

    Python3+Django get/post請求實現(xiàn)教程詳解

    這篇文章主要介紹了Python3+Django get/post請求實現(xiàn)教程詳解,需要的朋友可以參考下
    2021-02-02
  • 在 Windows 下搭建高效的 django 開發(fā)環(huán)境的詳細(xì)教程

    在 Windows 下搭建高效的 django 開發(fā)環(huán)境的詳細(xì)教程

    這篇文章主要介紹了如何在 Windows 下搭建高效的 django 開發(fā)環(huán)境,本文通過一篇詳細(xì)教程實例代碼相結(jié)合給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 關(guān)于pandas-profiling的降級之旅

    關(guān)于pandas-profiling的降級之旅

    這篇文章主要介紹了關(guān)于pandas-profiling的降級之旅,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Python 安裝 virturalenv 虛擬環(huán)境的教程詳解

    Python 安裝 virturalenv 虛擬環(huán)境的教程詳解

    這篇文章主要介紹了Python 安裝 virturalenv 虛擬環(huán)境的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • python pandas生成時間列表

    python pandas生成時間列表

    這篇文章主要介紹了python pandas生成時間列表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-06-06
  • 利用Python制作一個簡單的天氣播報系統(tǒng)

    利用Python制作一個簡單的天氣播報系統(tǒng)

    最近天氣的多變,好幾次出門半路天氣轉(zhuǎn)變。本文將利用python整個天氣爬蟲來獲取天氣情況。這樣也好可以進(jìn)行一個提前預(yù)防,感興趣的可以動手試一試
    2022-05-05
  • 深入了解python裝飾器

    深入了解python裝飾器

    這篇文章主要介紹了解python裝飾器,裝飾器定義一個為其他函數(shù)添加功能的函數(shù),裝飾器就是在不修改裝飾對象源代碼以及調(diào)用方式的前提下,為裝飾對象添加新功能,下面和小編一起進(jìn)入文章內(nèi)容了解更多知識,需要的朋友可以參考一下
    2022-03-03

最新評論