I'm using Django's URLconf, the URL I will receive is /?code=authenticationcode
I want to match the URL using r'^\?code=(?P<code>.*)$'
, but it doesn't work.
我正在使用Django的URLconf,我将接收到的URL是/?代码= authenticationcode我想匹配的URL使用r ' ^ \ ?代码=(? P <代码> 。*)的美元,但是它不工作。
Then I found out it is the problem of '?'.
Becuase I tried to match /aaa?aaa
using r'aaa\?aaa'
r'aaa\\?aaa'
even r'aaa.*aaa'
, all failed, but it works when it's "+" or any other character.
How to match the '?', is it special?
然后我发现这是‘?’的问题。因为我试着匹配/aaa?aaa使用r 'aaa \ ?aaa的r 'aaa \ \ ?aaa”甚至r 'aaa。*aaa',所有的都失败了,但是当它是“+”或任何其他字符时,它仍然有效。如何匹配?”,它是特殊的吗?
5 个解决方案
#1
13
>>> s="aaa?aaa"
>>> import re
>>> re.findall(r'aaa\?aaa', s)
['aaa?aaa']
The reason /aaa?aaa
won't match inside your URL is because a ?
begins a new GET query.
原因/ aaa吗?aaa不会匹配你的URL是因为a ?开始一个新的GET查询。
So, the matchable part of the URL is only up to the first 'aaa'. The remaining '?aaa' is a new query string separated by the '?' mark, containing a variable "aaa" being passed as a GET parameter.
因此,URL的可匹配部分只能达到第一个“aaa”。剩下的”?aaa'是由'分隔的一个新的查询字符串吗?' mark,包含作为GET参数传递的变量“aaa”。
What you can do here is encode the variable before it makes its way into the URL. The encoded form of ?
is %3F
.
您可以在变量进入URL之前对其进行编码。编码形式的?是f % 3。
You should also not match a GET query such as /?code=authenticationcode
using regex at all. Instead, match your URL up to /
using r'^$'
. Django will pass the variable code
as a GET parameter to the request
object, which you can obtain in your view using request.GET.get('code')
.
您也不应该匹配GET查询,例如/?代码=使用regex的验证代码。相反,匹配你的URL /使用r ^ $。Django将变量代码作为GET参数传递给请求对象,您可以使用request.GET. GET ('code')在视图中获得该对象。
#2
2
You are not allowed to use ?
in a URL as a variable value. The ?
indicates that there are variables coming in.
不允许你使用?在URL中作为变量值。的吗?表示有变量进入。
Like: http://www.example.com?variable=1&another_variable=2
如:http://www.example.com?variable=1&another_variable=2
Replace it or escape it. Here's some nice documentation.
要么替换它,要么转义它。这里有一些不错的文档。
#3
1
Django's urls.py
does not parse query strings, so there is no way to get this information at the urls.py
file.
Django url。py不解析查询字符串,因此无法在url中获取这些信息。py文件。
Instead, parse it in your view:
相反,在你的视图中解析它:
def foo(request):
code = request.GET.get('code')
if code:
# do stuff
else:
# No code!
#4
0
"How to match the '?', is it special?" Yes, but you are properly escaping it by using the backslash. I do not see where you have accounted for the leading forward slash, though. That bit just needs to be added in:
“如何匹配”?”,它是特殊的吗?”是的,但是你可以通过使用反斜线来正确地转义它。我不知道你在哪里解释了前斜杠。这一点需要加进去:
r'^/\?code=(?P<code>.*)$'
#5
0
supress the regex metacharacters with []
将regex元字符与[]
>>> s
'/?code=authenticationcode'
>>> r=re.compile(r'^/[?]code=(.+)')
>>> m=r.match(s)
>>> m.groups()
('authenticationcode',)
#1
13
>>> s="aaa?aaa"
>>> import re
>>> re.findall(r'aaa\?aaa', s)
['aaa?aaa']
The reason /aaa?aaa
won't match inside your URL is because a ?
begins a new GET query.
原因/ aaa吗?aaa不会匹配你的URL是因为a ?开始一个新的GET查询。
So, the matchable part of the URL is only up to the first 'aaa'. The remaining '?aaa' is a new query string separated by the '?' mark, containing a variable "aaa" being passed as a GET parameter.
因此,URL的可匹配部分只能达到第一个“aaa”。剩下的”?aaa'是由'分隔的一个新的查询字符串吗?' mark,包含作为GET参数传递的变量“aaa”。
What you can do here is encode the variable before it makes its way into the URL. The encoded form of ?
is %3F
.
您可以在变量进入URL之前对其进行编码。编码形式的?是f % 3。
You should also not match a GET query such as /?code=authenticationcode
using regex at all. Instead, match your URL up to /
using r'^$'
. Django will pass the variable code
as a GET parameter to the request
object, which you can obtain in your view using request.GET.get('code')
.
您也不应该匹配GET查询,例如/?代码=使用regex的验证代码。相反,匹配你的URL /使用r ^ $。Django将变量代码作为GET参数传递给请求对象,您可以使用request.GET. GET ('code')在视图中获得该对象。
#2
2
You are not allowed to use ?
in a URL as a variable value. The ?
indicates that there are variables coming in.
不允许你使用?在URL中作为变量值。的吗?表示有变量进入。
Like: http://www.example.com?variable=1&another_variable=2
如:http://www.example.com?variable=1&another_variable=2
Replace it or escape it. Here's some nice documentation.
要么替换它,要么转义它。这里有一些不错的文档。
#3
1
Django's urls.py
does not parse query strings, so there is no way to get this information at the urls.py
file.
Django url。py不解析查询字符串,因此无法在url中获取这些信息。py文件。
Instead, parse it in your view:
相反,在你的视图中解析它:
def foo(request):
code = request.GET.get('code')
if code:
# do stuff
else:
# No code!
#4
0
"How to match the '?', is it special?" Yes, but you are properly escaping it by using the backslash. I do not see where you have accounted for the leading forward slash, though. That bit just needs to be added in:
“如何匹配”?”,它是特殊的吗?”是的,但是你可以通过使用反斜线来正确地转义它。我不知道你在哪里解释了前斜杠。这一点需要加进去:
r'^/\?code=(?P<code>.*)$'
#5
0
supress the regex metacharacters with []
将regex元字符与[]
>>> s
'/?code=authenticationcode'
>>> r=re.compile(r'^/[?]code=(.+)')
>>> m=r.match(s)
>>> m.groups()
('authenticationcode',)