1

I've been working with this url for username regex in Django for a while:

url(r'^.../(?P<username>[-\w]+)/$'

But now, I've got a strange case, sometimes Google answers with a username like this:

luke.skywalker instead of lukeskywalker

And it looks like my regex doesn't accept dots - I get a NoReverseMatcherror. Could someone please help me with a correct regex?

halfer
  • 19,824
  • 17
  • 99
  • 186
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
  • 1
    Try: `[a-zA-Z.]+` and you may want to check the first answer [here](http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid) – Gocht May 26 '15 at 17:55
  • 1
    Note that google does the non-standard behavior that luke.skywalker@gmail.com and lukeskywalker@gmail.com are in fact the same email address. Is your problem that you're not matching them (which Daniel Roseman's answer should handle and also get the dash/underscore/numbers that Gocht's comment answer won't], or that you also need to "normalize" things so that luke.skywalker and lukeskywalker get treated as the same username? [I actually prefer the alternative such that I can create a relatively large # of test accounts from the same email address but I could see how normal might be annoyed] – Foon May 26 '15 at 18:19
  • @Foon Thanks for the info. I'm using Python Social Auth for the record (just in case someone finds this usefull). – Alejandro Veintimilla May 26 '15 at 20:52

2 Answers2

8

You can just add the characters you want to accept inside the square brackets:

r'^.../(?P<username>[-\w.]+)/$'
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
2

Simply add lookup_value_regex to your ViewSet:

class UserViewSet(viewsets.ModelViewSet):
      lookup_value_regex =  r"[\w.]+"

Source

alexrogo
  • 522
  • 3
  • 17