Django 进阶 之 自定义Form

Django中在定义表单时可以使用自带的Form模块来简化定义的数据验证

1 from django.conf.urls import url
2 from . import views
3 
4 urlpatterns = [
5     url(r'^$', views.home, name='home'),
6     url(r'^add/$', views.add, name='add'),
7 ]
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Django进阶教程</title>
 6 </head>
 7 <body>
 8 <p><b></b></p>
 9 <div>
10     {% for poem in poemlist %}
11         {{ poem.title }} - {{ poem.author }}<br>
12     {% endfor %}
13 </div>
14 </body>
15 </html>
/polls/templates/home.html
1 from django.shortcuts import render, redirect
2 from polls.models import ToDo, Poem
3 from .forms import AddForm
4 
5 def home(request):
6     poemlist = Poem.objects.all()
7     return render(request, 'home.html',{'poemlist':poemlist})
/poolls/views.py
1 from django.db import models
2 
3 class Poem(models.Model):
4     author = models.CharField(max_length=200)
5     title = models.CharField(max_length=200)
/polls/models.py
1 from django import forms
2 from .models import Poem
3 
4 class AddForm(forms.Form):
5     author = forms.CharField(label='作者', min_length=1, max_length=10)
6     title = forms.CharField()
/polls/forms.py

 

一、 基本使用方式

 1 from django.db import models
 2 from django.core.validators import ValidationError
 3 
 4 def validate_pre(value):
 5     if not value.startswith('a'):
 6         print('invalid')
 7         raise ValidationError('u must start with a', code='invalid')
 8 
 9 class Poem(models.Model):
10     author = models.CharField(max_length=200, validators=[validate_pre])
11     title = models.CharField(max_length=200)
/polls/models.py
 1 from django import forms
 2 from .models import Poem
 3 from django.core.validators import ValidationError
 4 
 5 class AddForm(forms.ModelForm):
 6     class Meta:
 7         model = Poem
 8         fields = ['author','title']
 9 
10     def clean_author(self):
11         data = self.cleaned_data['author']
12         print('clean_author')
13         if 'allen' not in data:
14             raise ValidationError('not has allen')
15         return data
/polls/forms.py

 

二、 使用ModelForm

1 from django import forms
2 from .models import Poem
3 
4 class AddForm(forms.ModelForm):
5     class Meta:
6         model = Poem
7         fields = ['author','title']
/polls/forms.py

 

三、 使用form添加验证, 方式一

 1 from django.db import models
 2 from django.core.validators import ValidationError
 3 
 4 
 5 def validate_pre(value):
 6     if not value.startswith('a'):
 7         print('invalid')
 8         raise ValidationError('u must start with a', code='invalid')
 9 
10 class Poem(models.Model):
11     author = models.CharField(max_length=200, validators=[validate_pre])
12     title = models.CharField(max_length=200)
13 
14     def __str__(self):
15         return '%s  %s' %(self.author, self.title)
/polls/models.py

 

四、 使用form添加验证, 方式二

 1 from django import forms
 2 from .models import Poem
 3 from django.core.validators import ValidationError
 4 
 5 
 6 class AddForm(forms.ModelForm):
 7     class Meta:
 8         model = Poem
 9         fields = ['author','title']
10 
11     def clean_author(self):
12         data = self.cleaned_data['author']
13         print('clean_author')
14         if 'allen' not in data:
15             raise ValidationError('not has allen')
16         return data
/polls/forms.py

 

五、 使用form添加验证, 方式三

 1 from django import forms
 2 from .models import Poem
 3 from django.core.validators import ValidationError
 4 
 5 class AddForm(forms.ModelForm):
 6     class Meta:
 7         model = Poem
 8         fields = ['author','title']
 9 
10     def clean(self):
11         print('clean')
12         author = self.cleaned_data['author']
13         title = self.cleaned_data['title']
14         object = Poem.objects.filter(author=author, title=title)
15         if object:
16             raise ValidationError('dup')
/polls/models.py

 

posted @ 2017-05-30 09:52  taibai  阅读(141)  评论(0)    收藏  举报