Tuesday, June 28, 2011

forms and form processing


I need to add a form on my django website

so far this is what i have:

in my models.py:

.... import statements not shown here

('skype video anruf','Skype Video-
('skype anruf ','Skype Anruf'),

class ContactForm(forms.Form):

date_input = forms.CharField(max_length=30, required=True) #,
time_input = forms.CharField(max_length=30, required=True) # 14:00
- 22:00

choice_options = forms.ChoiceField(widget=RadioSelect,
choices=CONTACT_OPTIONS, required=True)

email = forms.EmailField(max_length=30, required=True)
telephone = forms.CharField(max_length=30, required=False)
skype = forms.CharField(max_length=30, required=False)

comment_text = forms.CharField(widget=forms.Textarea,

my views.py

from website.contact.models import ContactForm
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.mail import send_mail

def contact(request):

if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST

if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data

subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
sender = form.cleaned_data['sender']
cc_myself = form.cleaned_data['cc']

date_input = form.cleaned_data['date input']
time_input = form.cleaned_data['time input']
choice_options = form.cleaned_data['contact options']

telephone = form.cleaned_data['telefone']
email = form.cleaned_data['email'] # this is the sender's
skype = form.cleaned_data['skype']

comment_text = form.cleaned_data['comment text']

recipients = ['joung.kwon@lingua-group.org']
if cc_myself:
send_mail('subject', 'message', 'myemail@myemail.com',
['myemail@myemail.com', email], fail_silently=False)

return HttpResponseRedirect('/danke/')

form = ContactForm() # An unbound form

return render_to_response('beratungstermin.html', {'form': form},

And on the website I have all the fields inside the form tag
<form action="/mythankyoupage" method="post">
{{ forms.as_p }}

What I want to do basically is to add the fields for name, email, tel,
some of them being compulsory, so the form should not be processed if
there is some info. missing.

I also want to receive a confirmation email whenever a user sends
information through the page, to my "myemail@myemail.com" (this is not
working on my code either), and a "CC" button if the user wants a copy
of his/her own message.


Let me know if you need any details

PS: i copied most of the code from the official django tutorial at
https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs , but
somehow I do not receive any emails after hitting the submit button.
Also, even if I declared in my views.py that some fields are
"required=True", I can still hit the send button with no error
messages for those unfilled textfields.

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