1

I want to activate my users in my django server when they follow a link from their email with a token, like this:

http://127.0.0.1:8000/user-activation/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImdhYm8iLCJ1c2VyX2lkIjoxOSwiZW1haWwiOiJsYXRvcnJlZ2FiQGdtYWlsLmNvbSIsImV4cCI6MTQ2NDE3ODk1N30.5FDKdxKxWuOkqe3rMNE-eHwmtrlWpD7EZ-EXw0yQM3U/

I have this pattern

 url(r'^user-activation/(?P<id>\w+)/', views.UserActivation.as_view()),

But it's returning 404. I searched a lot and nothing seems to work. What am I doing wrong?

I couldn't find any answer that works reading

https://docs.djangoproject.com/es/1.9/ref/urls/

nor

https://docs.djangoproject.com/es/1.9/topics/http/urls/

Edit:

I've read the post

Learning Regular Expressions

But there was no example about receiving hyphens and dots at the same time, which was what caused my confusion.

The marked answer solved my problem. Thanks

Community
  • 1
  • 1
Gabo
  • 185
  • 3
  • 16

2 Answers2

6

Your token also includes . and - characters so you need your regex to match them also - it currently will only match word characters

url(r'^user-activation/(?P<id>[\w\.-]+)/', views.UserActivation.as_view()),
Sayse
  • 38,955
  • 14
  • 69
  • 129
  • No need to escape `.` in a character class. To make it more obvious, change the order to `[-.\w]`. – Jan May 25 '16 at 13:47
  • @Jan - Sure that works too, but I've had to work with developers before that edit code without understanding it so I like the security provided by escaping the dot :) – Sayse May 25 '16 at 13:48
4

\w in python regex will match any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. You had all kinds of characters like . and - that don't match, it's bound to fail.

You need [\w.-] to include them.

Check python doc on regex syntax.

Shang Wang
  • 21,362
  • 17
  • 65
  • 88
  • See the comment in @Sayse's answer - no need to escape the dot in a character class. – Jan May 25 '16 at 13:47