import binascii


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.db import IntegrityError, transaction
from django.conf import settings
from django.template.loader import render_to_string
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import Http404


from contact.forms import ContactForm

# Create your views here.

def contact(request):
    form_init = {
            'username': request.user.username,
            'ip_address': request.META.get('REMOTE_ADDR'),
            }

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            messages.success('Success!')
        else:
            messages.error('Boo! Hiss!')
    else:
        form = ContactForm(initial=form_init)

    return render(request, 'contact/contact.html', {'form': form})


import binascii


from django.shortcuts import render
from django.contrib import messages
from django.core.mail import send_mail


from contact.forms import ContactForm

# Create your views here.

def contact(request):
    form_init = {
            'username': request.user.username,
            'ip_address': request.META.get('REMOTE_ADDR'),
            'email': request.user.email,
            }

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            messages.success(request, 'Success!')
        else:
            messages.error(request, 'Boo! Hiss!')
    else:
        form = ContactForm(initial=form_init)

    return render(request, 'contact/contact.html', {'form': form})


Clean up import, pre-fill email address
Clean up import, pre-fill email address

If user is logged in, their email address is automatically filled in for them.

Also fixed an error with the messages usage, namely I forgot to pass in the request object.

  import binascii
  
  
- from django.shortcuts import render, redirect, get_object_or_404
+ from django.shortcuts import render
  from django.contrib import messages
- from django.db import IntegrityError, transaction
- from django.conf import settings
- from django.template.loader import render_to_string
  from django.core.mail import send_mail
- from django.core.urlresolvers import reverse
- from django.http import Http404
  
  
  from contact.forms import ContactForm
  
  # Create your views here.
  
  def contact(request):
      form_init = {
              'username': request.user.username,
              'ip_address': request.META.get('REMOTE_ADDR'),
+             'email': request.user.email,
              }
  
      if request.method == 'POST':
          form = ContactForm(request.POST)
          if form.is_valid():
-             messages.success('Success!')
+             messages.success(request, 'Success!')
          else:
-             messages.error('Boo! Hiss!')
+             messages.error(request, 'Boo! Hiss!')
      else:
          form = ContactForm(initial=form_init)
  
      return render(request, 'contact/contact.html', {'form': form})
  
  
Clean up import, pre-fill email address
## Code Before:
import binascii


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.db import IntegrityError, transaction
from django.conf import settings
from django.template.loader import render_to_string
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import Http404


from contact.forms import ContactForm

# Create your views here.

def contact(request):
    form_init = {
            'username': request.user.username,
            'ip_address': request.META.get('REMOTE_ADDR'),
            }

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            messages.success('Success!')
        else:
            messages.error('Boo! Hiss!')
    else:
        form = ContactForm(initial=form_init)

    return render(request, 'contact/contact.html', {'form': form})


## Instruction:
Clean up import, pre-fill email address
## Code After:
import binascii


from django.shortcuts import render
from django.contrib import messages
from django.core.mail import send_mail


from contact.forms import ContactForm

# Create your views here.

def contact(request):
    form_init = {
            'username': request.user.username,
            'ip_address': request.META.get('REMOTE_ADDR'),
            'email': request.user.email,
            }

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            messages.success(request, 'Success!')
        else:
            messages.error(request, 'Boo! Hiss!')
    else:
        form = ContactForm(initial=form_init)

    return render(request, 'contact/contact.html', {'form': form})

