直接上配置:
url配置
[root@ns2 app]# cat app/urls.py from django.conf.urls import patterns, include, url from web import views urlpatterns = patterns('', url(r'^$', views.test), )
视图函数配置
[root@ns2 web]# cat views.py from web.forms import ContactForm from django.http import HttpResponse from django.shortcuts import render_to_response def 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.py from django import forms from django.forms.extras.widgets import SelectDateWidget import datetime from 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 [email protected] ') 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.html <html> <head> <title>Contact us</title> </head> <body> <h1>Contact us</h1> {% if form.errors %} <p style="color: red;"> Please correct the error{{ form.errors|pluralize }} below. </p> {% endif %} <form action="" method="post"> <table> {{ form.as_table }} </table> <input type="submit" value="Submit"> </form> </body> </html>
以上只供参考,更多信息参见官网