Question

Im trying to understand the django-registration app, and now Im able to send emails to users with activation key. What Im not able to figure out is how to send the activation key back to server when the user clicks a link in his/her email.

class AbstractEmailUser(AbstractBaseUser, PermissionsMixin):
      .....
       .....
   def send_activation_email(self, email):  
    email = email   
    ctx_dict = { 'activation_key' : self.activation_key,
             'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                                                }

    subject = render_to_string('activation_email_subject.txt', ctx_dict)
    subject = ''.join(subject.splitlines())
    message = render_to_string('activation_email.txt', ctx_dict)

    send_mail(subject, message, 'gccFishing.com', [email],  fail_silently = False)

   def activate(self, activation_key):
    if SHA1_RE.search(activation_key):
        try:
        self.get(activation_key = activation_key)
        except:
        self.model.DoesNotExist
        return False
        if not self.activation_key_expired():
        self.is_active = True
        self.activation_key = self.model.ACTIVATED
        self.save()
        return self
    return False

What code should go inside activation_email.txt for creating a link that calls the activate method with activation_key ? Or is there a better way of doing it?

Any help will be awesome. Thanks

Was it helpful?

Solution

Try something like this in your email if you are using the sites django app:

http://{{ site.domain }}{% url registration_activate activation_key %}

Or else change site.domain with the DNS of your site. your

If I remember correct, django-registration already contains an example activation_email.txt in its templates, check it out.

Update

I don't think that you should put the activation login in your User class. You definitely cannot do {% url user.registration_activate activation_key %} since you must pass a View to the url template tag! You cannot create a link without a view function (or a CBV).

What you have to do is to create a view that searches the Profiles for the activation_key and activates it. Check the ActivationView method of django-registration / registration / backends / default / views.py.

If you want to implement custom logic to your app just sublclass ActivationView and use your class in the urls.py instead of the default (django-registration / registration / backends / default / urls.py): url(r'^activate/(?P<activation_key>\w+)/$', ActivationView.as_view(), name='registration_activate'),

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top