Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按regex匹配referer_Python_Regex_Django - Fatal编程技术网

Python 按regex匹配referer

Python 按regex匹配referer,python,regex,django,Python,Regex,Django,我想设置一个简单的通知,如果一个视图有一个特定的基引用 假设我降落在http://myapp.com/page/我来自http://myapp.com/other/page/1。这里是我的伪代码的一个示例,基本上是如果我来自任何页面/X,我想设置一个通知 我认为它可能类似于^r^myapp.com/other/page/$,但我不太熟悉如何在python中使用正则表达式 from django.http import HttpRequest def someview(request):

我想设置一个简单的通知,如果一个视图有一个特定的基引用

假设我降落在
http://myapp.com/page/
我来自
http://myapp.com/other/page/1
。这里是我的伪代码的一个示例,基本上是如果我来自任何页面/X,我想设置一个通知

我认为它可能类似于
^r^myapp.com/other/page/$
,但我不太熟悉如何在python中使用正则表达式

from django.http import HttpRequest
def someview(request): 
     notify = False
     ... # other stuff not important to question
     req = HttpRequest()
     test = req.META['HTTP_REFERER'] like "http://myapp.com/other/page*"
     # where * denotes matching anything past that point and the test returns T/F
     if test:
        notify = True

    return # doesn't matter here

这可能更像是一个“在这种情况下如何使用regex”,而不是一个django问题

你可以这样做:

import re
referrer = "http://myapp.com/other/page/aaa"
m = re.match("^http://myapp.com/other/page/(.*)", referrer)
if m:
    print m.group(1)

顺便说一下,既然你指出回报并不重要。。。实际上,它在web应用程序中非常重要。如果您没有在计划控制流结束时返回(例如:身份验证失败),而只是执行重定向,那么您可能会受到不希望的影响:-)是的,我的意思是返回“…”之后的内容在这里并不重要。也就是说,这与这个问题无关。