如何使用reverse()将Django Request中的Referer URL与另一个URL进行比较?

时间:2015-08-11 14:27:23

标签: python django django-urls

如何比较引用网址和reverse()网址?

这是我目前的代码:

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!'

我不知道这是否是最好的方法。任何建议都会很棒。

1 个答案:

答案 0 :(得分:4)

您可以使用urlparse从网址获取路径元素。在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
相关问题