Wednesday, August 31, 2011

This is nuts why dont I get POST?

Model

from django.db import models

# Create your models here.

roof_choices = (
("South", "South"),
("South East", "South East"),
("South West", "South West"),
("East", "East"),
("West", "West"),
("North", "North")
)

pv_choices = (
("0.1-5 kWph", "0.1-5 kWph"),
("1-2 kWph", "1-2 kWph"),
("2-3 kWph", "2-3 kWph"),
("3-4 kWph", "3-4 kWph"),
("4-10 kWph", "4-10 kWph"),
(">10kWph", ">10kWph")
)

hear_choices = (
("Leaflet", "Leaflet",),
("Google", "Google"),
("Bing", "Bing"),
("Yahoo", "Yahoo"),
("Other Search Engine", "Other Search Engine"),
("Newspaper", "Newspaper"),
("Yellow Pages", "Yellow Pages"),
("Company Vehicle", "Company Vehicle"),
("Recommendation", "Recommendation"),
("TV", "TV"),
("Radio", "Radio"),
("Existing Customer", "Existing Customer"),
("Telephone Call", "Telephone Call"),
("Other", "Other")
)

class Capture(models.Model):
title = models.CharField(max_length=10)
initial = models.CharField(max_length=5)
surname= models.CharField(max_length=30)
email = models.EmailField()
telephone = models.CharField(max_length=30)
add1 = models.CharField(max_length=100)
add2 = models.CharField(max_length=100, null=True, blank=True)
town_city = models.CharField(max_length=100, null=True, blank=True)
county = models.CharField(max_length=100, null=True, blank=True)
pcode = models.CharField(max_length=30, null=True, blank=True)
roof = models.CharField(choices = roof_choices, max_length=25)
pv = models.CharField(choices = pv_choices, max_length=25)
heard = models.CharField(choices = hear_choices, max_length=50)

def __unicode__(self):
return self.email


View

from django import forms
from django.forms import ModelForm
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.forms.util import ValidationError
from django.core.mail import send_mail
from django.conf import settings
from smartpages.models import SmartPage
import re
from django.template import RequestContext
from capture.models import Capture, pv_choices, hear_choices, roof_choices

class CaptureForm(ModelForm):
title = forms.CharField(required=True, label = "Title")
initial = forms.CharField(required=True, label = "Initial")
surname = forms.CharField(required=True, label="Surname")
email = forms.EmailField(required=True, label="Email Address")
telephone = forms.CharField(required=True, label="Telephone")
add1 = forms.CharField(required=True, label="Address 1")
add2 = forms.CharField(required=False, label="Address 2")
town_city = forms.CharField(required=True, label="Town/City")
pcode = forms.CharField(required=True, label="Post Code")
county = forms.CharField(required=True, label="County")
roof = forms.ChoiceField(choices=roof_choices, required=True,
label="Roof Orientation")
pv = forms.ChoiceField(choices=pv_choices, required=True, label="PV
Size")
heard = forms.ChoiceField(choices=hear_choices, required=True,
label="Heard of Us?")
class Meta:
model = Capture

def capture(request):
if request.POST:
raise NameError("XXXXXXXXXXXXXXX")
form = CaptureForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
initial = form.cleaned_data['initial']
surname = form.cleaned_data['surname']
email = form.cleaned_data['email']
telephone = form.cleaned_data['telephone']
add1 = form.cleaned_data['add1']
if form.cleaned_data['add2']:
add2 = form.cleaned_data['add2']
town_city = form.cleaned_data['town_city']
county = form.cleaned_data['county']
roof = form.cleaned_data['roof']
pv = form.cleaned_data['pv']
heard = form.cleaned_data['heard']
msg_header = "Form content\n\n"
msg_middle = "Name: %s %s %s" % (form.cleaned_data['title'],
form.cleaned_data['initial'], form.cleaned_data['surname'])
msg_middle += "Email: %s\n\n" % form.cleaned_data['email']
msg_middle += "Telephone: %s\n\n" %
form.cleaned_data['telephone']
send_mail(settings.EMAIL_SUBJECT_PREFIX + "Email from the
capture detail form to use the calculator", msg_header+msg_middle,
settings.DEFAULT_FROM_EMAIL, [settings.CONTACT_EMAIL_TO,],
fail_silently=False)
return HttpResponseRedirect('/cost-and-earnings-calculator/')
else:
form = CaptureForm(request.POST)
else:
form = CaptureForm()
try:
content = SmartPage.objects.get(slug="calculator")
except SmartPage.DoesNotExist:
content = None
return render_to_response("capture/form.html", {"form": form, "content":
content, }, context_instance=RequestContext(request))


Template

{% extends "base.html" %}

{% block extrahead %}
{% block meta_title %}<title>{% if content.meta_title
%}{{content.meta_title}}{% else %}{% if title %}{{title}}{% else %}Default
title - change me{% endif %}{% endif %}</title>{% endblock %}
{% block meta_desc %}<meta name="description" content="{% if
content.meta_desc %}{{content.meta_desc}}{% else %}Default description -
change me{% endif %}" />{% endblock %}
{% block meta_keywords %}<meta name="keywords" content="{% if
content.meta_keywords %}{{content.meta_keywords}}{%else%}Default, keywords,
change, me{%endif%}" />{% endblock %}

{% endblock %}
{% block midimage %} {{ content.headimage.headimage.url }} {% endblock %}
{% block content %}
<div>{{ content.content }}</div>
<div>
<form action="." method=post" id="captureForm">
{{ form.as_p}}
<input type="submit" value="Continue" id="captureSubmitButton" />
</form>
</div>
{% endblock %}
--
View this message in context: http://old.nabble.com/This-is-nuts-why-dont-I-get-POST--tp32372381p32372381.html
Sent from the django-users mailing list archive at Nabble.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment