-------------------------------------第一部分-----------------------------------------------------------------------------------
#主要app
from flask import Flask,render_template
import config
from exts import db
from models import User,Article,Tag
#導入藍圖
from articleview import article_bp
app = Flask(__name__)
app.config.from_object(config)
#注冊藍圖
app.register_blueprint(article_bp)
db.init_app(app)
@app.route('/')
def hello_world():
#插入用戶名
user = User(username='zhiliao',email='xxx@qq.com')
#插入文章
article = Article(title='abc',content='123')
#abc這個文章是zhiliao寫的
article.author = user
tag1 = Tag(name='前端')
tag2 = Tag(name='Python')
#abc這個文章的標簽有 前端、和Python
article.tags.append(tag1)
article.tags.append(tag2)
#只需要添加article 那么和article相關聯的所有對象就會被統一添加
db.session.add(article)
#提交
db.session.commit()
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True,port = 9999)
-------------------------------------------第二部分、藍圖中使用 flask-restful發送是數據庫數據、json數據的格式--------------------------------------------------------------------
from flask import Blueprint,make_response,render_template,make_response
from flask_restful import Resource,fields,marshal_with,Api
from models import Article
'''
使用ajax給前端進行傳值
'''
#使用藍圖
article_bp = Blueprint("article",__name__,url_prefix="/article")
#使用藍圖初始化app
api = Api(article_bp)
#當訪問 article/list的時候要判斷返回什么數據,是什么類型? 如果是get請求給他返回html,
# @api.representation("text/html")
# def output_html(data,code,headers):
#
# '''
# 在這個函數里面判斷 是什么數據? json 還是 html
# :param data:
# :param code:
# :param headers:
# :return: 在representation裝飾的函數中, 必須返回一個Response對象
# '''
# # print(data)
# print("data什么類型:",type(data))
# if type(data) == str:
# resp = make_response(data)
# return resp
# else:
# # return data
# pass
class ArticleView(Resource):
#驗證要傳遞的數據
resource_fields = {
# attribute 的意思是修改名稱 把title修改成school
"school": fields.String(attribute="title"),
"content":fields.String,
# "author":fields.String,
"author":fields.Nested({
"username":fields.String,
"email":fields.String
}),
# "tags":fields.String 需要傳遞列表
"tags":fields.List(fields.Nested({
"id":fields.Integer,
"name":fields.String
})),
# default 指定是80
"read_count":fields.Integer(default=80)
}
#
@marshal_with(resource_fields)
def get(self,article_id):
#拿數據
article = Article.query.get(article_id)
#傳遞數據
print("----------",article)
return article
api.add_resource(ArticleView,"/<article_id>/",endpoint = "article")
class ListView(Resource):
def get(self):
#繼承了 Resource的 視圖,默認返回json數據,這里返回html 所以要用到11-29行代碼
return render_template("index.html")
# return{"username":"wakaka"}
api.add_resource(ListView,"/list/",endpoint = "list")
------------------------------------------------第三部分、前端頁面如果數據存在 打印一個標簽-------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{# 引入 script#}
<script src="../static/jq/jquery-2.1.0.js"></script>
<script>
$(function(){
$.ajax({
type: "get",
{# 數據來自於 17 行#}
url: "article/1/",
data: {
},
{#格式#}
dataType: "json",
success: function(data){
{#判斷字典里面這個值是否存在,如果存在生成一個標簽#}
var sc= data.school;
if(sc!=null&&sc!=''){
var html = '<span>'+sc+'</span>';
$("#bo").html(html);
}
{#簡單的傳參1、---------------------------------------------------------------#}
{#控制台打印#}
console.log(data.author.email);
{#html頁面顯示#}
$("#span1").html(data.author.email);
var aa = "<tr><td>xxxxxxxxx</td><td>qqqqqq</td></tr>";
{##給 id = #d1進行傳遞數據#}
$("#id1").html(data.read_count);
$("#id2").html(data.author.username);
$("#tab").html(aa);
}
});
});
</script>
</head>
<body>
{#<p>我是從模板當中渲染的</p>#}
{#<span id="span1">這是現實郵箱的</span>#}
{##}
{#<p id = "id1"></p>#}
{##}
{#<span>這是通過jquery渲染數據</span><p id = "id2"></p>#}
{##}
{#<table id="tab" border="1" cellpadding="0" cellspacing="0"></table>#}
<hr>
<p id="bo"></p>
</body>
</html>