Codium
Codium

Reputation: 3240

Another NoReverseMatch error. Trying to use reverse() in model layer

I tried to find similar topic but any does not help me much.

My error (e.g.):

Reverse for 'user_activate' with arguments '(u'pU6ARMPCgHeyTAGhBWYwxZqLSUtwqWHqnf9MNEfd',)' and keyword arguments '{}' not found

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)

What I am doing wrong here?

Upvotes: 1

Views: 201

Answers (1)

Rohan
Rohan

Reputation: 53386

You have named the url as name='user-activate' (with dash), while you are trying to reverse lookup with 'user_activate' with '_'.

Change

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

to

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

Upvotes: 3

Related Questions