jazzband / django-newsletter

An email newsletter application for the Django web application framework, including an extended admin interface, web (un)subscription, dynamic e-mail templates, an archive and HTML email support.
GNU Affero General Public License v3.0
851 stars 206 forks source link

No subscription / user context variable available in template for Personalization #368

Open mrxsal opened 3 years ago

mrxsal commented 3 years ago

(beginner django developer here) It seems to me there is no subscription context variable available in the message.html template. image (screenshot is from django-debug-toolbar)

How would one go about with email personalization? something like: Hi {{ user.first_name }} or, Hi {{ subscription.email }}.

I would expect the subscription context variable to be available to use as a standard.

Related Question : How would I be able to add my own custom context variables to this? I see that I am not able to to use my own custom context processors in the template, such as {{ user }} Say I own airbnb.com and I want to show 5 recommended Listings. How could I include a "Listing" context variable?

Sorry if this is not the place to ask these questions.

mrxsal commented 3 years ago

Forgive me for the noobiness. I realize that the subscription instance is added to the template's context in the Submission model when send_message() is run. That being said, I would appreciate some help understanding how I can add my own context variables to this. In the send_message method sits the "variable dict", which I hope to append my own context variables to. I don't know if this is the right way to go, but I'm trying monkey-patching. I can't seem to be able to access the "variable dict" unless I add it directly in the source code.

DJANGO_NEWSLETTER_CONTEXT_PATCH = True  # adds context to Submission

def monkey_patch():
    if DJANGO_NEWSLETTER_CONTEXT_PATCH:
        from newsletter.models import Submission
        old_send_message = Submission.send_message

        def new_send_message(self, *k, **kw):
            old_send_message(self, *k, **kw)
            # old_send_message.variable_dict['test'] = 'testvalue'  # add more context here (gives error)

        Submission.send_message = new_send_message

Can you give me some pointers to solve this? it's really appreciated.