Search This Website

Loading...

Friday, 16 October 2009

Create a FORM in Django

FORM OBJECTS: put the code below in models.py 
from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)
 
Using a FORM in a VIEW: put the code below in views.py 
def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            form.save() # Will save the POST data in the Database
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })
FORMS FIELD TYPES GO HERE 
 
Displaying a form using template 
{{ form.as_p }}

0 comments:

Post a Comment

Recent Comments