Im trying to write a function for getting form values and their label so I can have them in a email body like this:
<p><strong>Labelvalue</strong>: formvalue</p>
Is that possible?
When I do “body = smart_unicode(form.cleaned_data)” I get the dict, but Im not sure what I could do to get the html like I want.
forms.py
class MyForm(forms.Form):
TYPE_CHOICES = (
(ADULT, 'Adult'),
(CHILD, 'Child'),
)
type = forms.ChoiceField(widget=RadioSelect, choices=TYPE_CHOICES, label='Adult or child')
name = forms.CharField(label='Name')
birthdate = forms.DateField(widget=SelectDateWidget(years=range(2012,1900,-1)), label='Birthdate')
address = forms.CharField(label='Address')
email = forms.EmailField(label='Email')
Views.py
def show_myform(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
subject = "Testsubject"
sender = form.cleaned_data['email']
recipients = ['post@mydomain.com']
body = smart_unicode(form.cleaned_data)
msg = EmailMessage(subject, body, sender, recipients)
msg.send()
return HttpResponseRedirect('/thanks/')
else:
form = MyForm()
return render(request, 'form/myform.html', {
'form': form,
})
You could tray to render the html you need into the body variable. You also need a html template for your mail body:
You can pass whatever you need to your template context dictionary (here i used var1 and var2 for example). Then send mail like this:
Hope this leads into the right direction.