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.
This commit is contained in:
Jan Dittberner 2016-01-09 14:46:04 +00:00
parent b8893e92d7
commit de501cfdde
2 changed files with 9 additions and 4 deletions

View file

@ -52,11 +52,16 @@ class ContactForm(forms.Form):
self.request, dict(self.cleaned_data, site=site)) self.request, dict(self.cleaned_data, site=site))
def message(self): 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): def subject(self):
subject = loader.render_to_string( context = self.get_context().flatten()
self.subject_template_name, self.get_context()) subject = loader.render_to_string(self.subject_template_name, context)
return ''.join(subject.splitlines()) return ''.join(subject.splitlines())
def save(self, fail_silently=False): def save(self, fail_silently=False):

View file

@ -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 sent the following message via the contact form at
{{ site }}{% url 'contact_form' %}: {{ site }}{% url 'contact_form' %}: