Foreplay:
FromData: Three ways to get FromData
1. Create a FromData object, and then use the append method to add key-value pairs
var formdata = new FormData();
formdata.append('name','mihon');
formdata.append('url','/app01/login');
2. Get the form element object and pass it into the FormData object as a parameter!
var formobj = document.getElementById('form');
var formdata = new FormData(formobj);
3. Generated using the getFormData method of the form element object.
var formobj = document.getElementById('form');
var formdata = formobj.getFormData()
ajax upload file:
1. processData is set to false. Because the data value is a FormData object, there is no need to process the data
2. The contentType is set to false, and the contentType value is not set, because it is a FormData object constructed by the <form> form,
And the attribute enctype="multipart/form-data" has been declared, so it is set to false here.
3. Add the enctype="multipart/form-data" attribute to the <form> tag
4. If cache is set to false, uploading files does not require caching.
eg.
<form id="uploadForm" enctype="multipart/form-data">
<input id="file" type="file" name="file"/>
<button id="upload" type="button">upload</button>
</form>
$.ajax({
url: '/upload',
type: 'POST',
cache: false,
data: new FormData($('#uploadForm')[0]),
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
source link:http://www.cnblogs.com/wupeiqi/articles/6144178.html
import module:
from django.froms import From
from django.froms import widgets
from django.froms import fields
1. Create a template:
class MyForm(From):
user = fields.CharField(min_length=6)
1. Field
Field:
require = True, is it required
widget = {}, plugin
label=None, used to generate Label labels or display content
initial=None, initial value
help_text='', help information (displayed next to the label)
error_messages=None, error message {'required': 'cannot be empty', 'invalid': 'format error'}
show_hidden_initial=False, whether to add a hidden plug-in with a default value after the current plug-in (can be used to check whether the two inputs are consistent)
validators=[], custom validation rules
localize=False, whether to support localization
disabled=False, whether it can be edited
label_suffix=None Label content suffix
CharField(Field)
max_length=None, maximum length
min_length=None, minimum length
strip=True whether to remove user input whitespace
IntegerField(Field)
max_value=None, maximum value
min_value=None, minimum value
DecimalField(IntegerField)
max_value=None, maximum value
min_value=None, minimum value
max_digits=None, total length
decimal_places=None, decimal place length
RegexField(CharField)
regex, custom regular expression
max_length=None, maximum length
min_length=None, minimum length
error_message=None, ignore, use error_messages={'invalid': '...'} for error messages
BaseTemporalField(Field)
input_formats=None time formatting
DateField(BaseTemporalField) 格式:2015-09-01
TimeField(BaseTemporalField) Format: 11:12
DateTimeField(BaseTemporalField)格式:2015-09-01 11:12
DurationField(Field) time interval: %d %H:%M:%S.%f
...
RegexField(CharField)
regex, custom regular expression
max_length=None, maximum length
min_length=None, minimum length
error_message=None, ignore, use error_messages={'invalid': '...'} for error messages
ModelChoiceField(ChoiceField)
... django.forms.models.ModelChoiceField
queryset, # query the data in the database
empty_label="---------", # default empty display content
to_field_name=None, # Field corresponding to the value of value in HTML
limit_choices_to=None # Secondary filtering of queryset in ModelForm
eg:
Use the ModelChoiceField and ModelMultipleChoiceField fields provided by django to achieve
from django import forms
from django.forms import fields
from django.forms import widgets
from django.forms import models as form_model
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
class FInfo(forms.Form):
authors = form_model.ModelMultipleChoiceField(queryset=models.NNewType.objects.all())
Custom validation rules:
from django.forms import Form
from django.forms import widgets
from django.forms import fields
from django.core.validators import RegexValidator
class MyForm(Form):
user = fields.CharField(
validators=[RegexValidator(r'^[0-9]+$', 'Please enter a number'), RegexValidator(r'^159[0-9]+$', 'Number must start with 159')],
)
3. Plugins
widgets.RadioSelect(choices=((1,'SH'),(2,'BJ'),))
widgets.SelectFiled(choices=((1,'SH'),(2,'BJ'),)) #Radio select
widgets.MultipleChoiceFiled(choices=((1,'SH'),(2,'BJ'),)) #多选select
2.View function processing:
obj = MyForm(request.Post) #Get the MyFrom object, which encapsulates the data sent by the front end
obj.is_valid() #The rule validation returns True, otherwise it returns False
obj.clean() #Get the data submitted by the front-end after the verification is passed
obj.errors() #Validation failed, get error information
obj.errors.as_json() #Return json type error information
obj.errors.data() #return direct data type
3.HTML template processing :
{{obj.user}} #Render the input label
{{obj.user.errors}}#Render li tag with error message
{{obj.errors.user.0}} #Get the string of error information, <span>{{obj.errors.user.0}}<span/>
Other tags:
{{obj.user.label}}#Generate label label <!label = 'username', default label = 'False'>
{{obj.user.label.id_for_label}}
{{obj.user.label_tag}} #label_tag label, select the input label when clicking the label
Custom validation rules:
from django import forms
form django.forms import fields
form django.forms import widgets
from django.core.exceptions import ValidationError
form django.core.validators import RegexValidator
class FInfo(forms.Form):
username = fields.CharField(max_length=5,
vaidators=[RegexValidator(r'^[0-9]+$','Enter a ')])