Django: urlpattern for username?

So far, the team assistant has used this code for user url patterns:

# urls.py urlpatterns = patterns('...', url(r'^user/(?P<username>[.-_\w]+)/foo', 'myapp.views.foo'), .... 

there is a hidden error: if the username contains - , then the reverse will fail because the beginning of the regular expression pattern [.-_ means "all characters from . to _ ".

What pattern can be used to match valid all names?

PS: I think adding a character - not enough in regex if you want to combine all possible usernames in django.

+6
source share
5 answers

I don't think you should put any kind of username validation in your URL pattern. Keep your verification in one place - the place where you create your accounts for the first time.

You must map everything that the user supplies there, and pass this to the safe database function to search for the username and fail if it does not exist.

So, in your url template, let the browser send something non-empty and rely on your very smart database to tell you what you decided earlier or not.

 url(r'^user/(?P<username>.+)/foo$', 'myapp.views.foo'), 

Also note the "$" at the end.

+5
source

Based on what I see in the AbstractUser model, I think the best regular expression used to capture a username is (?P<username>[\ w.@ +-]+) .

+7
source

You can move the hyphen to the beginning of the character class,

 [-.\w] 

or you can avoid it with a backslash

 [.\-\w] 

Note. I removed the underscore as it is included in \w . I also assume that you only want to accept . , - and \w , and you do not want to accept all characters from . to _ . This range includes characters like @ , so you can verify that all of your usernames match the new regular expression.

+4
source

You can use the following method:

[-.\w] ( - use most on the left)

or [.\-\w] ( - use backslash anywhere)

or [.\w-] ( - use in most cases)

if you use special characters, then it is best to use \ (backslash) before any special characters (which are used in the special regex char. expression).

For better use, your regex would be ^user/(?P<username>[.\-_\w]+)/foo

+3
source

First of all, this is not an error, but the function is well documented in the documents :

[]

Used to indicate a character set. In the set:

Character ranges can be specified by specifying two characters and dividing them by "-", for example, [az] will match any lowercase ASCII letter, [0-5] [0-9] will correspond to all two-digit characters, numbers from 00 to 59, and [0-9A-Fa-f] will match any hexadecimal digit. If - is escaped (for example, [az]), or if it is placed as the first or last character (for example, [a-]), it will match the literal '-' .

So using - between two literals will evaluate this regular expression as a range of characters:

 re.compile("[a-0]+") >> error: bad character range re.findall("[.-_]+", "asdasd-asdasdad._?asdasd-") >> ['._?'] 

As you can see, python will always be interperet - as a range indicator when used between characters in character sets.

As it is (also) indicated in documents, avoiding the declaration of a range, it is done by escaping - using \- or by placing it as the first or last literal in the character set []

If you want to capture this range of characters, including - , try:

 re.findall("[.-_\-]+", "asdasd-asdasdad._?asdasd-") >> ['-', '._?', '-'] 

Note: \w is [a-zA-Z0-9_] when the LOCALE and UNICODE flags are not set. Therefore you do not need to declare _ again

And in your situation:

 url(r'^user/(?P<username>[-.\w]+)/foo', 'myapp.views.foo') url(r'^user/(?P<username>[.\w-]+)/foo', 'myapp.views.foo') url(r'^user/(?P<username>[.\-\w]+)/foo', 'myapp.views.foo') 

Besides using - if you are using the default Django Username style, then @ navneet35371 is right about a valid character set. You can change your regex character set to include @ and + and use

 url(r'^user/(?P<username>[\ w.@ +-]+)/foo', 'myapp.views.foo') 
0
source

All Articles