brown1001
brown1001

Reputation: 677

Contact Form Django Not Working

I am trying to create a contact form for my Django site but it's not working properly. There are three steps to the contact form. Step 1 has a box where you input the subject of the email. Step 2 has a box where you input the sender's email address. At this point, there are three buttons- "first step", "prev step", and "submit". If I click "submit", the site doesn't take me to step 3, which is supposed to be where you input the body of the email. Instead, it reroutes me back to the Step 1 page.

I did my research and I can't find anything online related to this particular problem.

Here is my views.py file, which is located in the django_test/django_test directory:

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from forms import MyRegistrationForm 
from django.contrib.formtools.wizard.views import SessionWizardView
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
#import logging
#logr = logging.getLogger(__name__)

def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('login.html', c)

def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/loggedin')
    else:
        return HttpResponseRedirect('/accounts/invalid')

def loggedin(request):
    return render_to_response('loggedin.html',
                          {'full_name': request.user.username})

def invalid_login(request):
    return render_to_response('invalid_login.html')

def logout(request):
    auth.logout(request)
    return render_to_response('logout.html')

def register_user(request):
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')

    args = {}
    args.update(csrf(request))

    args['form'] = MyRegistrationForm()

    return render_to_response('register.html', args)

def register_success(request):
    return render_to_response('register_success.html')

class ContactWizard(SessionWizardView):
    template_name = "contact_form.html"

    def done(self, form_list, **kwargs):
        form_data = process_form_data(form_list)

        return render_to_response('done.html', {'form_data': form_data})

def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]

    logr.debug(form_data[0]['subject'])
    logr.debug(form_data[1]['sender'])
    logr.debug(form_data[2]['message'])

    send_mail(form_data[0]['subject'],
          form_data[2]['message'], form_data[1]['sender'],
          [(my email address], fail_silently=False)

    return form_data 

This my forms.py file, also located in the django_test/django_test directory:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class MyRegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']

        if commit:
            user.save()
        return user 

class ContactForm1(forms.Form):
    subject = forms.CharField(max_length=100)

class ContactForm2(forms.Form):
    sender = forms.EmailField()

class ContactForm3(forms.Form):
    message = forms.CharField(widget=forms.Textarea)

And my contact_form.html file, located in the django_test/templates directory:

{% extends "base.html" %}

{% block content %}

    <h2>Contact Us</h2>
    <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
    {% for field in form %}
        {{field.error}}

    {% endfor %}

    <form action="/contact/" method="post">{% csrf_token %}
    <table>
    {{ wizard.management_form }}
    {% if wizard.form.forms %}
        {{ wizard.form.management_form }}
        {% for form in wizard.form.forms %}
            {{ form }}
        {% endfor %}
    {% else %}
        {{ wizard.form }}
    {% endif %}
    </table>
    {% if wizard.steps.prev %}
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">"first step"</button>
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">"prev step"</buttom>
    {% endif %}

    <input type="submit" value="submit" />

    </form>

{% endblock %}

And this is my urls.py file:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

from django_test.api import ArticleResource

from django_test.forms import ContactForm1, ContactForm2, ContactForm3
from django_test.views import ContactWizard

article_resource = ArticleResource()

urlpatterns = patterns('',
                   url(r'^admin/', include(admin.site.urls)),
                   url(r'^accounts/login/$', 'django_test.views.login'),
                   url(r'^accounts/auth/$', 'django_test.views.auth_view'),
                   url(r'^accounts/loggedin/$', 'django_test.views.loggedin'),
                   url(r'^accounts/invalid/$', 'django_test.views.invalid_login'),
                   url(r'^accounts/logout/$', 'django_test.views.logout'),
                   url(r'^accounts/register/$', 'django_test.views.register_user'),
                   url(r'^accounts/register_success/$', 'django_test.views.register_success'),
                   url(r'^articles/all/$', 'article.views.articles'),
                   url(r'^articles/create/$', 'article.views.create'),
                   url(r'^articles/get/(?P<article_id>\d+)/$', 'article.views.article'),
                   url(r'^articles/like/(?P<article_id>\d+)/$', 'article.views.like_article'),
                   url(r'^articles/add_comment/(?P<article_id>\d+)/$', 'article.views.add_comment'),
                   url(r'^articles/search/', 'article.views.search_titles'),
                   url(r'^articles/api/', include(article_resource.urls)),
                   url(r'^contact/', ContactWizard.as_view([ContactForm1, ContactForm2, ContactForm3])),

)

I'm not getting any error messages either, which is frustrating, so I don't know what I'm doing wrong. Thank you.

Upvotes: 1

Views: 536

Answers (1)

brown1001
brown1001

Reputation: 677

All I had to do was view the page source on the site. It turned out

  <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev  }}">"prev step"</buttom>

in contact_form.html had a typo: </buttom>. I fixed the error and now I get the comment page.

The line should be:

  <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">"prev step"</button>

Upvotes: 1

Related Questions