Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Django url regex for username that accepts dots

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?

Upvotes: 1

Views: 3813

Answers (2)

alexrogo
alexrogo

Reputation: 662

Simply add lookup_value_regex to your ViewSet:

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

Source

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599470

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

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

Upvotes: 8

Related Questions