12

im using django-registration, all is fine, the confirmation email was sending in plain text, but know im fixed and is sending in html, but i have a litter problem... the html code is showing:

<a href="http://www.example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/">http://www. example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/</a>

and i dont need to show the html code like the ...

Any idea?

Thanks

4 Answers 4

27

To avoid patching django-registration, you should extend the RegistrationProfile model with proxy=True:

models.py

class HtmlRegistrationProfile(RegistrationProfile):
    class Meta:
        proxy = True
    def send_activation_email(self, site):
        """Send the activation mail"""
        from django.core.mail import EmailMultiAlternatives
        from django.template.loader import render_to_string

        ctx_dict = {'activation_key': self.activation_key,
                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                    'site': site}
        subject = render_to_string('registration/activation_email_subject.txt',
                                   ctx_dict)
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        message_text = render_to_string('registration/activation_email.txt', ctx_dict)
        message_html = render_to_string('registration/activation_email.html', ctx_dict)

        msg = EmailMultiAlternatives(subject, message_text, settings.DEFAULT_FROM_EMAIL, [self.user.email])
        msg.attach_alternative(message_html, "text/html")
        msg.send()

And in your registration backend, just use HtmlRegistrationProfile instead of RegistrationProfile.

Sign up to request clarification or add additional context in comments.

4 Comments

How do i register the new profile with the registration backend?
How do I set the backend to HtmlRegistration profile instead of RegistrationProfile?
Do we have to make an other registration backend that uses our new proxy Model ?
How you can set the new default?
15

I'd recommend sending both a text version and an html version. Look in the models.py of the django-registration for :

send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])

and instead do something like from the docs http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

3 Comments

Yes Paul, thans for replay, but is not working i did in that way and nothing...but now is ok :) , now just a put the link without <a ...
That will send a text email that some clients will create links for. If you ever need more interesting html, you will have to do what I recommended.
yes, i tryed but dont work, but is ok :) ill try a litter more :)
2

I know this is old and the registration package is no longer maintained. Just in case somebody still wants this. The additional steps wrt to the answer of @bpierre are:
- subclass the RegistrationView, i.e. your app's views.py

class MyRegistrationView(RegistrationView):
...
def register(self, request, **cleaned_data):
    ...
    new_user = HtmlRegistrationProfile.objects.create_inactive_user(username, email, password, site)

- in your urls.py change the view to the sub-classed view, i.e. - List item

url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'

1 Comment

re 'the registration package is no longer maintained': Are we talking about the same package? The OP refers to django-registration, and it has commits as recent as two months ago. github.com/ubernostrum/django-registration Also, what's HtmlRegistrationProfile? Can you include the import statement, and which package you're referring to? Again, unless I'm missing something, it doesn't seem to be django-registration.
0

This guy have extended the defaultBackend enabling us to add an HTML version of the activation email.

Specifically, the alternate version job is done here

I managed to use the backend part successfully

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.