另一个NoReverseMatch错误。试图在模型层中使用reverse()

时间:2012-10-22 09:04:41

标签: django django-models django-urls

我试图找到类似的主题,但任何对我都没有帮助。

我的错误(例如):

  

使用参数反转'user_activate'   '(u'pU6ARMPCgHeyTAGhBWYwxZqLSUtwqWHqnf9MNEfd',)'和关键字参数   找不到“{}”

urls.py:

url(r'^user/activate/([A-Za-z0-9]+)$','users.views.activate', name='user-activate'),

views.py:

def activate(request, key):
    user = User.objects.select_related('profile').get(activation_key=key)
    if user is not None:
       if user.is_active:
           print 'already active'
       else:
          if user.get_profile().check_key_is_valid():
             user.is_active = True
             user.save()
          else:
             user.get_profile().reset_activation_data()
             send_activation_email(user.get_profile().activation_key, user.email)
             print 'key expired'
    else:
        print 'user not found'
    return HttpResponseRedirect(reverse('front'))

models.py:

def send_activation_email(key, email):
    from django.core.urlresolvers import reverse
    path = reverse('user_activate', args=[key])
    from django.contrib.sites.models import Site
    site = Site.objects.get_current().domain
    url = "http://%s%s" % (site, path)

    from django.core.mail import send_mail
    from my_app import settings
    send_mail('Account activation', url, settings.SITE_EMAIL,
        [email], fail_silently=False)

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

当您尝试使用name='user-activate' 'user_activate'反向查找时,您已将网址命名为'_'(使用短划线)。

更改

path = reverse('user_activate', args=[key])

path = reverse('user-activate', args=[key])