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,471
  • 17
  • 87
  • 173
Alejandro Veintimilla
  • 9,492
  • 22
  • 87
  • 168
  • 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

7

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

r'^.../(?P<username>[-\w.]+)/$'
Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
2

Simply add lookup_value_regex to your ViewSet:

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

Source

alexrogo
  • 316
  • 1
  • 13