Вопрос

I'm using django-registration for a project of mine.

I'd like to add some extra contextual data to the template used for email activation.

Looking into the register view source, I cannot figure out how to do it.

Any idea ?

Это было полезно?

Решение

From what I remember, you need to write your own registration backend object (easier then is sounds) as well as your own profile model that inherits from RegistrationProfile and make the backend use your custom RegistrationProfile instead (This model is where the email templates are rendered and there is no way to extend the context, so they need to be overwritten)

Другие советы

A simple solution is to rewrite the send_activation_email So instead of

registration_profile.send_activation_email(site)

I wrote this in my Users model

def send_activation_email(self, registration_profile):
    ctx_dict = {
        'activation_key': registration_profile.activation_key,
        'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
        'OTHER_CONTEXT': 'your own context'
    }
    subject = render_to_string('registration/activation_email_subject.txt',
                               ctx_dict)
    subject = ''.join(subject.splitlines())
    message = render_to_string('registration/activation_email.txt',
                               ctx_dict)
    self.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)

And I call it like this

user.send_activation_email(registration_profile)

I don't get what it is your problem but the parameter is just in the code you link (the last one):

def register(request, backend, success_url=None, form_class=None,
         disallowed_url='registration_disallowed',
         template_name='registration/registration_form.html',
         extra_context=None)

That means you can do it from wherever you are calling the method. Let's say your urls.py:

from registration.views import register

(...)

url(r'/registration/^$', register(extra_context={'value-1':'foo', 'value-2':'boo'})), name='registration_access')

That's in urls.py, where usually people ask more, but, of course, it could be from any other file you are calling the method.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top