Django名字组Urls.py

时间:2014-03-29 22:37:19

标签: python django

我正在尝试为用户帐户电子邮件激活系统创建网址映射。注册后,将向用户发送一封电子邮件,其中包含要关注的网址。该链接采用 / login / activate / confirmation_code /用户名的形式。我想弄清楚在Django url调度程序中使用命名组,所以我可以在我的视图中将confirmation_code和username作为参数传递给def。但是,我似乎无法弄明白。感谢您的任何帮助。 我一直在引用:https://docs.djangoproject.com/en/dev/topics/http/urls/

my urls.py:

from django.conf.urls import patterns, url
from login import views

urlpatterns = patterns('',
     url(r'^register/$', views.registration, name='index.html'),
     url(r'^authenticate/$', views.authenticateLogin, name='index.html'),
     url(r'^authenticateUser/$', views.authenticateUser, name='index.html'),
     url(r'^activate/(?P<confirmation_code>\d{4}/?P<username>\d{4})/$', views.activate)
)

错误消息:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^blogadmin/
^ ^$ [name='index']
^ ^about/$ [name='about']
^ ^contact/$ [name='contact']
^ ^$ [name='index']
^login/ ^register/$ [name='index.html']
^login/ ^authenticate/$ [name='index.html']
^login/ ^authenticateUser/$ [name='index.html']
^login/ ^activate/(?P<confirmation_code>\d{4})/(?P<username>\d{4})/$ [name='activate']
^blog/
^webstore/
^companion/
The current URL, login/activate/o5y0WONgVpyXELEIuNg9xp3lCVod35JDB/pdf2e, didn't match any   of these.

感谢karthikr和Jingo的答案

分辨率:

url(r'^activate/(?P<confirmation_code>[\w]+)/(?P<username>[\w]+/)$', views.activate, {}, 'activate')

1 个答案:

答案 0 :(得分:1)

您已将指定组的括号设置为错误,请尝试如下:

url(r'^activate/(?P<confirmation_code>[\w]{1,33})/(?P<username>\[\w]+)/$', 'views.activate', {}, 'activate')

HTH:)

更新: 如果您的令牌最多为33个字符(字母数字),请尝试上述操作。