[英]How to compare Referer URL in Django Request to another URL using reverse()?
如何比較referer URL和reverse()
url?
這是我目前的代碼:
if request.META.get('HTTP_REFERER') == reverse('dashboard'):
print 'Yeah!'
但這不起作用,因為反向輸出/dashboard
而HTTP_REFERER
輸出http://localhost:8000/dashboard/
我目前的解決方案是:
if reverse('dashboard') in request.META.get('HTTP_REFERER'):
print 'Yeah!'
我不知道這是否是最好的方法。 任何建議都會很棒。
您可以使用urlparse
從URL獲取path元素。 在Python3中:
from urllib import parse
path = parse.urlparse('http://localhost:8000/dashboard/').path
在Python 2中:
import urlparse
path = urlparse.urlparse('http://localhost:8000/dashboard/').path
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.