直接上配置:

url配置

[root@ns2 app]# cat app/urls.pyfrom django.conf.urls import patterns, include, urlfrom  web import  viewsurlpatterns = patterns('',url(r'^$', views.test),)

视图函数配置

[root@ns2 web]# cat views.pyfrom web.forms import ContactFormfrom django.http import HttpResponsefrom django.shortcuts import render_to_responsedef test(request):if request.method == 'POST':form = ContactForm(request.POST)if form.is_valid():subject = form.cleaned_data['subject']message = form.cleaned_data['message']email = form.cleaned_data['email']return HttpResponse("%s%s%s"%(subject , message , email))else:form = ContactForm()return render_to_response('contact_form.html', {'form': form})

表单项配置

[root@ns2 web]# cat  forms.pyfrom django import formsfrom django.forms.extras.widgets import SelectDateWidgetimport datetimefrom django.forms import widgets"""more information about django's forms can  see https://docs.djangoproject.com/en/1.6/ref/forms/fields/#required"""BIRTH_YEAR_CHOICES = ('1980', '1981', '1982')FAVORITE_COLORS_CHOICES = (('blue', 'Blue'),('green', 'Green'),('black', 'Black'))CHOICES = (('1', 'First',), ('2', 'Second',))class ContactForm(forms.Form):test=forms.MultiValueField()ip =forms.IPAddressField()day = forms.DateField(input_formats=['%d %B, %Y'],initial=datetime.date.today)url = forms.URLField(initial='http://www.badiu.com')subject = forms.CharField(max_length=100,error_messages={'required': 'Please enter send to who '})email = forms.EmailField(required=False ,label='Your e-mail address',help_text='email address as a@a.com ')message = forms.CharField(widget=forms.Textarea)time = forms.CharField(widget=forms.TimeInput)task_name = forms.CharField(max_length=50, required=True)description = forms.CharField(max_length=200, required=False)project = forms.CharField(max_length=50, required=False)context = forms.CharField(max_length=50, required=False)deadline = forms.DateTimeField(widget=forms.DateTimeInput, required=False)CheckboxInput=forms.ChoiceField(widget=forms.CheckboxInput)Select = forms.ChoiceField(widget=forms.Select,choices=CHOICES)NullBooleanSelect=forms.ChoiceField(widget=forms.NullBooleanSelect)SelectMultiple=forms.ChoiceField(widget=forms.SelectMultiple,choices=CHOICES)RadioSelect=forms.ChoiceField(widget=forms.RadioSelect,choices=CHOICES)CheckboxSelectMultiple=forms.ChoiceField(widget=forms.CheckboxSelectMultiple,choices=CHOICES)FileInput=forms.ChoiceField(widget=forms.FileInput)ClearableFileInput=forms.ChoiceField(widget=forms.ClearableFileInput)MultipleHiddenInput=forms.ChoiceField(widget=forms.MultipleHiddenInput)SplitDateTimeWidget=forms.ChoiceField(widget=forms.SplitDateTimeWidget)#SplitHiddenDateTimeWidget=forms.SplitDateTimeField(widget=forms.SplitHiddenDateTimeWidget)#SelectDateWidget=forms.ChoiceField(widget=forms.SelectDateWidget)birth_year = forms.DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES))favorite_colors = forms.MultipleChoiceField(required=False,widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)named = forms.TextInput(attrs={'size': 10, 'title': 'Your name',})named.render('name','aaa name')name = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))urll = forms.URLField()comment = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))te=forms.TextInput(attrs={'size': 10, 'title': 'Your name',})def clean_message(self):message = self.cleaned_data['message']num_words = len(message.split())num_words = len(message.join(message.split()))if num_words < 2:raise forms.ValidationError("Not enough words!")return message

模板内容配置:

[root@ns2 app]# cat  templates/contact_form.htmlContact us

Contact us

{% if form.errors %}

Please correct the error{

{ form.errors|pluralize }} below.

{% endif %}
` form`.`as_table `

以上只供参考,更多信息参见官网