使用Django和Flask獲取訪問來源referrer
Flask
request.referrer # 來路
request.headers.get('User-Agent') # 請求頭
Django
request.META['HTTP_REFERER'] # 來路
request.META.get("HTTP_USER_AGENT") # 請求頭
補充:flask 重定向到上一個頁面,referrer、next參數(shù) --
>重定向會上一個頁面
在某些場景下,我們需要在用戶訪問某個url后重定向會上一個頁面,比如用戶點擊某個需要登錄才能訪問的連接,這時程序會重定向到登錄頁面,當(dāng)用戶登錄后比較合理的行為是重定向到用戶登錄前瀏覽的頁面。
下面的例中,在foo和bar視圖中生成連接,鏈接過去后,沒有重定向會上一個頁面
@app.route('/foo')
def foo():
return '<h1>Foo page </h1><a href="%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Do something</a>' %url_for('do_something')
@app.route('/bar')
def bar():
return '<h1>Bar page</h1><a href="%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Do something </a>' % url_for('do_something')
@app.route('/do_something')
def do_something():
return redirect(url_for('hello'))
@app.route('/hello')
def hello():
name = request.args.get('name')
if name is None:
name = request.cookies.get('name','xiaxiaoxu')#從cookie中獲取name值
response = '<h1>Hello, %s</h1>' % name
return response
if __name__ == '__main__':
app.run(debug = True)
結(jié)果:
訪問127.0.0.1:5000/foo或者127.0.0.1:5000/bar后,頁面出現(xiàn)連接,點擊鏈接后,進入hello頁面,之后停留在了hello頁面

點擊鏈接后重定向到了hello頁面

我們的目的是在鏈接后,返回原來的頁面
重定向會上一個頁面,關(guān)鍵是獲取上一個頁面的URL。
獲取上一個頁面的URL有兩種方法:
HTTP referrer
HTTP referrer是一個用來記錄請求發(fā)源地址的HTTP首部字段(HTTP_REFERER),即訪問來源。當(dāng)用戶在某個站點點擊鏈接,瀏覽器想新鏈接所在的服務(wù)器發(fā)起請求,請求的數(shù)據(jù)中包含的HTTP_REFERER字段記錄了用戶所在的原站點URL。
在flask中,referer的值可以通過請求對象的referrer屬性獲取,即request.referrer
修改do_something視圖函數(shù):
@app.route('/do_something')
def do_something():
return redirect(request.referrer)
在bar頁面上再次點擊鏈接

有的時候,referrer字段可能是空值,比如用戶直接在瀏覽器地址欄輸入URL或者因為防火墻或者瀏覽器設(shè)置自動清除或修改referer字段,我們需要添加一個備選項:
return redirect(request.referrer or url_for('hello'))
查詢參數(shù)next
除了自動從referrer獲取,另一種更常見的方式是在URL中手動加入包含當(dāng)前頁面URL的查詢參數(shù),這個查詢參數(shù)一般命名為next
在bar視圖中的鏈接do_something對應(yīng)的視圖添加next參數(shù)(在/do_someghing后加參數(shù))
def bar():
#print dir(request)
print "request.full_path:",request.full_path
#print "request.url:",request.url
return '<h1>Bar page</h1><a href="%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Do something and redirect </a>' % url_for('do_something', next = request.full_path)
@app.route('/do_something')
def do_something():
return redirect(request.args.get('next'))

為了避免next參數(shù)為空的情況,也可以加備選項,如果為空就重定向到hello視圖
return redirect(request.args.get('next', url_for('hello')))
為了覆蓋更全面,可以將查詢參數(shù)next和referrer兩種方式結(jié)合起來使用:
先獲取next參數(shù),如果為空就嘗試獲取referer,如果仍然為空,就重定向到默認(rèn)的hello視圖
因為在不同視圖執(zhí)行這部分操作的代碼相同,我們可以創(chuàng)建一個通用的函數(shù)redirect_back()函數(shù)
在do_something視圖中調(diào)用這個函數(shù)
@app.route('/bar')
def bar():
print "request.full_path:",request.full_path
return '<h1>Bar page</h1><a href="%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Do something and redirect </a>' % url_for('do_something', next = request.full_path)
def redirect_back(default = 'hello',**kwargs):
for target in request.args.get('next'),request.referrer:
if target:
return redirect(target)
return redirect(url_for(default,**kwargs))
@app.route('/do_something_and_redirect')
def do_something():
return redirect_back()
if __name__ == '__main__':
app.run(debug = True)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
詳解Python 定時框架 Apscheduler原理及安裝過程
Apscheduler是一個非常強大且易用的類庫,可以方便我們快速的搭建一些強大的定時任務(wù)或者定時監(jiān)控類的調(diào)度系統(tǒng),這篇文章主要介紹了Python 定時框架 Apscheduler ,需要的朋友可以參考下2019-06-06
在Python的Flask框架中實現(xiàn)全文搜索功能
這篇文章主要介紹了在Python的Flask框架中實現(xiàn)全文搜索功能,這個基本的web功能實現(xiàn)起來非常簡單,需要的朋友可以參考下2015-04-04
python3.9和pycharm的安裝教程并創(chuàng)建簡單項目的步驟
這篇文章主要介紹了python3.9和pycharm的安裝教程并創(chuàng)建簡單項目的步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

