django contact page send_email
<p>I am trying to create a contact page which will take 17 inputs from a visitor and then email that information to me. I found many basic tutorials but none specific to what I am trying to achieve. So far this is what I have:</p>

<p>I created a new Django project "contactform" then a new app "send_email"</p>

<p>This is my forms.py file located- send_email/forms.py</p>

<pre><code>from django import forms

class ContactForm(forms.Form):
    title = forms.CharField(max_length=3, required=True)
    first_name = forms.CharField(required=True)
    last_name = forms.CharField(required=True)
    identity_type = forms.CharField(required=True)
    identity_number = forms.IntegerField(required=True)
    current_job = forms.CharField(required=True)
    career_prospects = forms.CharField(required=True)
    age = forms.IntegerField(required=True)
    nationality = forms.CharField(required=True)
    address = forms.CharField(required=True)
    city = forms.CharField(required=True)
    province = forms.CharField(required=True)
    postal_code = forms.IntegerField(required=True)
    contact_number = forms.IntegerField(required=True)
    daytime_contact_number = forms.IntegerField(required=True)
    evening_contact_number = forms.IntegerField(required=True)
    email_address = forms.EmailField(required=True)
</code></pre>

<p>My views.py file located- send_email/views.py</p>

<pre><code>from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import ContactForm

def email(request):
if request.method == 'GET':
    form = ContactForm()
else:
    form = ContactForm(request.POST)
    if form.is_valid():
        title = form.cleaned_data['title']
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        identity_type = form.cleaned_data['identity_type']
        identity_number = form.cleaned_data['identity_number']
        current_job = form.cleaned_data['current_job']
        career_prospects = form.cleaned_data['career_prospects']
        age = form.cleaned_data['age']
        nationality = form.cleaned_data['nationality']
        address = form.cleaned_data['address']
        city = form.cleaned_data['city']
        province = form.cleaned_data['province']
        postal_code = form.cleaned_data['postal_code']
        contact_number = form.cleaned_data['contact_number']
        daytime_contact_number = form.cleaned_data['daytime_contact_number']
        evening_contact_number = form.cleaned_data['evening_contact_number']
        email_address = form.cleaned_data['email_address']
        try:
            send_mail(first_name, message, email_address ['admin@example.com'])
        except BadHeaderError:
            return HttpResponse('Invalid header found.')
        return redirect('success')
return render(request, "email.html", {'form': form})

def success(request):
return HttpResponse('Success! Thank you for your message.')
</code></pre>

<p>email.html as follows:</p>

<pre><code>&lt;h1&gt;Contact Us&lt;/h1&gt;
&lt;form method="post"&gt;    
{% csrf_token %}
{{ form.as_ul }}
&lt;div class="form-actions"&gt;
    &lt;button type="submit"&gt;Send&lt;/button&gt;
&lt;/div&gt;  
</code></pre>

<p></p>

<p>Now I am well aware that Django send_mail take these arguments: (subject, message, sender, recipients) I just want to know if there is a way to pass the data I am asking for into the "message" parameter and email it as a list?</p>

<p>Here you go:</p>

<hr>

<p><strong>email.template.html</strong></p>

<pre><code>Hello, someone filled out a contact form with the following information:

First name: {{ first_name }}
Last name : {{ last_name }}
. . . and so on
</code></pre>

<p>-- or if youâre lazy like me --</p>

<pre><code>Hello, someone filled out a contact form with the following information:
{% for key, value in form.cleaned_data.items %}
{{ key }}: {{ value }}
{% endfor %}
</code></pre>

<hr>

<pre><code>from django.template.loader import get_template
from django.template import Context

def email(request):
    . . .
    context = Context(locals())
    template = get_template('email.template.html')
    message = template.render(context)
</code></pre>

python django django-forms contact-form