Why doesn't Django find these urls even though the regex matches?

The python docs for regex have a description of what "." does:

(Dot.) In default mode this matches any character except new line. If the DOTALL flag is specified, it matches any character, including a newline.

For a project I am doing in Django, I am setting this regex:

url(r'^accounts/confirm/(.+)$', confirm,name='confirmation_view')

      

For everyone I understand, this should match any url starting with "accounts / confirm /" and then the next number of arbitrary characters. Then these arbitrary characters are passed to the "confirm" function as a parameter. So far so good.

So this regex must match

accounts/confirm/fb75c6529af9246e4e048d8a4298882909dc03ee0/

      

as well as

accounts/confirm/fb75c6529af9246e4e-048d8a4298882909dc03ee0/

      

and

accounts/confirm/fb75c6529af9246e4e=048d8a4298882909dc03ee0/

      

and

accounts/confirm/fb75c6529af9246e4e%20048d8a4298882909dc03ee0/

      

This is at least what I thought it would do. But this is not so, it corresponds only to the first. Django keeps returning 404 to me on others. Which I don't understand because the (. +) Part of the expression must mean "match one or more characters other than newline".

edit As the comments and answers showed, I got the correct expression. So now the question is, why doesn't Django return the correct view, but 404. Does it force you to do something in the URL before passing it to this regex?

+3


source to share


1 answer


A quick test confirms that this should work:

>>>import re
>>>test = ["accounts/confirm/fb75c6529af9246e4e048d8a4298882909dc03ee0/", "accounts/confirm/fb75c6529af9246e4e-048d8a4298882909dc03ee0/", "accounts/confirm/fb75c6529af9246e4e=048d8a4298882909dc03ee0/", "accounts/confirm/fb75c6529af9246e4e%20048d8a4298882909dc03ee0/"]
>>>all([re.match(r'^accounts/confirm/(.+)$', item) for item in test])
True

      

This will return false for any matches:



>>>test.append("something else")
>>>all([re.match(r'^accounts/confirm/(.+)$', item) for item in test])
False

      

The problem must be elsewhere.

+8


source







All Articles