0

ビューに特定のベース リファラーがある場合、簡単な通知を設定したいと思います。

http://myapp.com/page/に着陸し、 から来たとしましょうhttp://myapp.com/other/page/1。これは私の疑似コードの例です。基本的に、任意のページ/XI から来て、通知をセットアップしたい場合です。

のようなものかもしれないと思っていますが^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

これは、特にdjangoの質問ではなく、「このコンテキストで正規表現を使用するにはどうすればよいですか」のようなものかもしれません。

4

1 に答える 1

2

次のようなものを使用できます。

import re
referrer = "http://myapp.com/other/page/aaa"
m = re.match("^http://myapp.com/other/page/(.*)", referrer)
if m:
    print m.group(1)
于 2013-03-05T00:47:05.297 に答える