From de501cfddebbe28d09dcfbd6794480c5e15a40be Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 9 Jan 2016 14:46:04 +0000 Subject: [PATCH] Fix Django deprecation warning Django 1.9 deprecates passing a context directly to the render function in template loader. This commit creates a proper template rendering context before rendering the mails sent from the contact form. --- gnuviechadmin/contact_form/forms.py | 11 ++++++++--- gnuviechadmin/templates/contact_form/contact_form.txt | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/gnuviechadmin/contact_form/forms.py b/gnuviechadmin/contact_form/forms.py index e023fe7..e531b93 100644 --- a/gnuviechadmin/contact_form/forms.py +++ b/gnuviechadmin/contact_form/forms.py @@ -52,11 +52,16 @@ class ContactForm(forms.Form): self.request, dict(self.cleaned_data, site=site)) def message(self): - return loader.render_to_string(self.template_name, self.get_context()) + context = self.get_context() + template_context = context.flatten() + template_context.update({ + 'remote_ip': context.request.META['REMOTE_ADDR'] + }) + return loader.render_to_string(self.template_name, template_context) def subject(self): - subject = loader.render_to_string( - self.subject_template_name, self.get_context()) + context = self.get_context().flatten() + subject = loader.render_to_string(self.subject_template_name, context) return ''.join(subject.splitlines()) def save(self, fail_silently=False): diff --git a/gnuviechadmin/templates/contact_form/contact_form.txt b/gnuviechadmin/templates/contact_form/contact_form.txt index 6adab33..b015009 100644 --- a/gnuviechadmin/templates/contact_form/contact_form.txt +++ b/gnuviechadmin/templates/contact_form/contact_form.txt @@ -1,4 +1,4 @@ -User {{ name }} <{{ email }}> from IP address {{ request.META.REMOTE_ADDR }} +User {{ name }} <{{ email }}> from IP address {{ remote_ip }} sent the following message via the contact form at {{ site }}{% url 'contact_form' %}: