I have a choice field like this in my forms.py:
ASSUNTO = ( ('1', u'Informações'),
('2', 'Comercial'),
('3', 'Financeiro'),
('4', 'Outro'))
In my views.py when I send the chosen value from the choice field as e-mail subject, it comes like:
Chose ‘Comercial‘ in my form in my html page
E-mail received with subject like: ‘2 – MY COMPANY‘
Code in my views.py:
from django.shortcuts import render
from forms import FormContato
from django.core.mail import send_mail
def HomeContato(request):
form = FormContato()
if request.method == 'POST':
form = FormContato(request.POST)
if form.is_valid():
send_mail( form.cleaned_data['assunto'] + ' - MY COMPANY', 'Nome: ' + form.data['nome'] + ' ' + form.data['sobrenome'] + ' :: ' + 'E-mail: ' + form.data['email'] + ' :: ' + 'Mensagem: ' + form.data['menssagem'], 'noreply@email.com.br', ['email@gmail.com'], fail_silently=False)
return render(request, 'contato/contato.html', locals())
else:
return render(request, 'contato/contato.html', locals())
else:
return render(request, 'contato/contato.html', locals())
How can I use this command getting the subject as: ‘Comercial – MY COMPANY’?
Thanks in advance to all!
This isnt very pretty, but the following should do it:
NOTE: after reading the other answer, you could do
dict(form.fields['assunto'].choices)[form.cleaned_data['assunto']]– I didnt think to use a builtin function instead of a comprehensionAlso the reason you are getting “2” is that the choices tuple is a tuple of value => label tuples.
if you want the value to not be a meaningless integer, make it like this:
and then keep your view the same as you currently have it