十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇內(nèi)容介紹了“flask sqlalchemy擴(kuò)展包有什么用”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)公司為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)公司核心團(tuán)隊十多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗,為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。
flask_sqlalchemy 擴(kuò)展包對 ORM 框架 SQLAlchemy 進(jìn)行了簡單封裝,對 Query對象封裝后支持分頁操作,就是 flask_sqlalchemy.BaseQuery.paginate 方法,傳入頁數(shù)和每頁大小就會返現(xiàn)對應(yīng)的 Pagination 對象。例如:
pagination = Article.query.join(Account) \
.filter(Article.status == 1) \
.order_by(Article.published_at.desc()) \
.paginate(page, per_page)
Pagination 對象里面有數(shù)據(jù),有數(shù)據(jù)總條數(shù)。這樣比自己實現(xiàn)分頁能節(jié)省幾行代碼,然后把代碼重構(gòu)成用 paginate 后,遇到個詭異的問題。發(fā)現(xiàn)訪問帶分頁參數(shù)的 URL 報 404 了,一開始以為是我輸入的 URL 不正確,然后打印 url_map 里面值,對比沒有毛病。 我又嘗試 把 paginate 去掉,還原成原來的 limit 和 offset 方法來控制分頁,這樣又恢復(fù)正常了。這樣我確定是這個 paginate 方法搞的鬼,然后就找到里面的源碼,發(fā)現(xiàn)該方法有個error_out 參數(shù)
def paginate(self, page=None, per_page=None, error_out=True, max_per_page=None):
"""Returns ``per_page`` items from page ``page``.
If ``page`` or ``per_page`` are ``None``, they will be retrieved from
the request query. If ``max_per_page`` is specified, ``per_page`` will
be limited to that value. If there is no request or they aren't in the
query, they default to 1 and 20 respectively.
When ``error_out`` is ``True`` (default), the following rules will
cause a 404 response:
* No items are found and ``page`` is not 1.
* ``page`` is less than 1, or ``per_page`` is negative.
* ``page`` or ``per_page`` are not ints.
When ``error_out`` is ``False``, ``page`` and ``per_page`` default to
1 and 20 respectively.
Returns a :class:`Pagination` object.
"""
if request:
if page is None:
try:
page = int(request.args.get('page', 1))
except (TypeError, ValueError):
if error_out:
abort(404)
page = 1
if per_page is None:
try:
per_page = int(request.args.get('per_page', 20))
except (TypeError, ValueError):
if error_out:
abort(404)
per_page = 20
else:
if page is None:
page = 1
if per_page is None:
per_page = 20
if max_per_page is not None:
per_page = min(per_page, max_per_page)
if page < 1:
if error_out:
abort(404)
else:
page = 1
if per_page < 0:
if error_out:
abort(404)
else:
per_page = 20
items = self.limit(per_page).offset((page - 1) * per_page).all()
if not items and page != 1 and error_out:
abort(404)
error_out 默認(rèn)值為 True,這段代碼的意思就是任何異常行為都會導(dǎo)致 404 響應(yīng),比如你傳入的page參數(shù)不是數(shù)字就報 404,如果小于1也報 404, 沒有數(shù)據(jù)時也會報 404。 我在是寫 API 接口, 你給我報 404,太特么不厚道了。
作為一個第三方庫,默認(rèn)給開發(fā)者做決定個人認(rèn)為并不妥當(dāng),因為你的本意可能是 dont make me think, 反而讓我陷入了沉思,因為我不知道為什么報 404,看不到任何堆棧日志,更合理的方式應(yīng)該是用異常的方式告知開發(fā)者。這樣我就知道怎么去修改參數(shù)來返回期望的結(jié)果。其實,這跟產(chǎn)品經(jīng)理設(shè)計產(chǎn)品是一樣的道理,對最終用戶我們有必要隱藏所有的技術(shù)細(xì)節(jié),任何錯誤日志都不應(yīng)該展示給用戶,因為用戶看不懂,也不需要讓它看懂。而錯誤日志對開發(fā)者來說卻至關(guān)重要,如果把這些日志也給我隱藏了,我不得不去深入源碼里面找原因。
“flask sqlalchemy擴(kuò)展包有什么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!