如何比较引荐来源网址和 reverse()
网址?
这是我当前的代码:
if request.META.get('HTTP_REFERER') == reverse('dashboard'):
print 'Yeah!'
但这不起作用,因为反向将输出 /dashboard
而 HTTP_REFERER
输出 http://localhost:8000/仪表板/
我目前的解决方案是:
if reverse('dashboard') in request.META.get('HTTP_REFERER'):
print 'Yeah!'
我不知道这是否是最好的方法。任何建议都会很棒。
最佳答案
您可以使用 urlparse
从 URL 中获取路径元素。在 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
关于python - 如何使用 reverse() 将 Django 请求中的 Referer URL 与另一个 URL 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944521/