from flask import flask
defsearch
(q, page):
"""[summary]
arguments:
q -- [普通關鍵字]
page
"""isbn_or_key = 'key'
if len(q) == 13
and q.isdigit(): # 判斷是否是isbn號碼
isbn_or_key = 'isbn'
short_q = q.replace('-', '')
# and的先後順序有影響,
越有可能是假的就放前面, 消耗資源的如查詢資料庫放後面
if'-'in q and len(short_q) == 10
and short_q.isdigit():
isbn_or_key = 'isbn'
pass
if __name__ == '__main__':
def
is_isbn_or_key
(word):
""" 判斷傳入引數是
關鍵字搜尋還是isbn編號
:param word:
:return: isbn_or_key
"""isbn_or_key = 'key'
if len(word) == 13
and word.isdigit():
isbn_or_key = 'isbn'
short_word = word.replace('-', '')
if'-'in word and len(short_word) == 10
and short_word.isdigit():
isbn_or_key = 'isbn'
return isbn_or_key
from flask import flask
from helper import is_isbn_or_key # 從helper.py匯入
defsearch
(q, page):
""" :param q:
:param page:
:return:
"""isbn_or_key = is_isbn_or_key(q) # 簡化了search檢視函式的**,方便閱讀
# 使用類
方便以後拓展
@staticmethod
defget
(url, returned_json=true):
""" :param url:
:param returned_json:
:return:
"""r = requests.get(url)
# 未簡化版
# if r.status_code == 200:
# if returned_json:
# return r.json() # 返回json格式
# else:
# return r.text # 返回原始字串
# else:
# if returned_json:
# return {}
# else:
# return ""
# 簡化**,儘量減少return語句
if r.status_code != 200: # 根據狀態碼判斷是否返回成功
return {} if returned_json else
''return r.json() if returned_json else r.text
from flask import flask
from helper import is_isbn_or_key
from yushu_book import yushubook
import json
defsearch
(q, page):
""" :param q:
:param page:
:return:
"""isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = yushubook.search_by_isbn(q)
else:
result = yushubook.search_by_keyword(q)
# 這裡result是dict,
我們要轉為json
if __name__ == '__main__':
from flask import flask, jsonify
from helper import is_isbn_or_key
from yushu_book import yushubook
import json
defsearch
(q, page):
""" :param q:
:param page:
:return:
"""isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = yushubook.search_by_isbn(q)
else:
result = yushubook.search_by_keyword(q)
# 這裡result是dict,
我們要轉為json
return jsonify(result) # jsonify簡化
if __name__ == '__main__':
from helper import is_isbn_or_key
from yushu_book import yushubook
from flask import jsonify
defsearch
(q, page):
""" :param q:
:param page:
:return:
"""isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = yushubook.search_by_isbn(q)
else:
result = yushubook.search_by_keyword(q)
return jsonify(result)
from flask import flask, jsonify
if __name__ == '__main__':
def
decorator
(f):
endpoint = options.pop('endpoint', none)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
if view_func is
notnone:
old_func = self.view_functions.get(endpoint)
if old_func is
notnone
and old_func != view_func:
'existing endpoint function: %s' % endpoint)
self.view_functions[endpoint] = view_func
from helper import is_isbn_or_key
from yushu_book import yushubook
from flask import jsonify
defsearch
(q, page):
""" :param q:
:param page:
:return:
"""isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = yushubook.search_by_isbn(q)
else:
result = yushubook.search_by_keyword(q)
return jsonify(result)
from flask import flask, jsonify
if __name__ == '__main__':
Flask 中的路由與反向路由
之後 定義乙個函式,該函式名也是用來給特定函式生成 urls,並且返回我們想要顯示在使用者瀏覽器上的資訊。from flask import flask,request,url for defhello world return hello world defhello user return he...
Flask 路由系統
反向生成url def index print index print url for n1 return index 動態路由 def index nid print url for index nid 777 print url for index nid nid 根據輸入的nid,反向生成ur...
flask框架 路由
1.利用methods限制訪問方式 render template 返回頁面 request 一切瀏覽器請求的內容都封裝到request物件中 request.method 來判斷訪問方式 method 設定請求訪問方式 from flask import flask,render template...