【问题标题】:How to compare Referer URL in Django Request to another URL using reverse()?如何使用reverse()将Django Request中的Referer URL与另一个URL进行比较?
【发布时间】:2015-11-03 20:11:05
【问题描述】:

如何比较referer URL和reverse() url?

这是我当前的代码:

if request.META.get('HTTP_REFERER') == reverse('dashboard'):
    print 'Yeah!'

但这不起作用,因为反向将输出 /dashboardHTTP_REFERER 输出 http://localhost:8000/dashboard/

我目前的解决方案是:

if reverse('dashboard') in request.META.get('HTTP_REFERER'):
    print 'Yeah!'

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

【问题讨论】:

    标签: python django django-urls


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 2014-02-18
      相关资源
      最近更新 更多