--- --- restore content begins
1. In the new version, add app is set directly in the settings, add INSTALLED_APPS in the app name,
But his writing is complete 'app01.apps.App01Config' because the new version is optimized, so the name also write directly to app support
2.form label transfer files
form form default encoding format is not supported urlencoded issued a document (only the file name sent in the past)
it corresponds to the data format username = jason & password = 123
1. Specify the encoding format form submission. Enctype added in the form tag = "multipart / form-data" operation can be achieved using the form tag file transfer
2. Set the input file type in the tag will appear Select File column
After using post a request submitted to the background, the background using request.FILES.get () to get the file will get a file object, the object can be used. Name method to get the file name
3. ajax submission
In the bootstrap introduced jQuery jQuery in Boot. Placed in the <head>
1. input tag type is set to file. The Select File column
2. Set a button button
2. Click Settings ajax event and then use FormData () generate a built-in object
This object can either transmit normal data (key data) can also transfer files
Ordinary data:
Use variable forData.append ( 'name', 'owen'); adding the normal data
send file:
1. The first obtain the file object $ ( '# d2') [0] .files [0], to obtain the object tag, taking the index turn into native objects js built-in methods,, reuse file object files obtained
2. reuse formData.append ( 'myfile', the file object) is added to the key way in formData,
Then sent via ajax formData to the background, the background using request.FILES.get () Get file using request.POST.get () to get the normal data
forms components:
About test scripts written in django
Py create a new file, copy the following code to the manage.py py file
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djtest.settings")
Add the following code
imput django
django.setup()
Then into the app modles
from app01 imput modelsz
Another method is explained below again pycharm has a python Console, to help you get over the operation in the environment
forms components all fields are mandatory default, parameters passed will have to have less need to pass the position parameter tips
In the assembly form data validation rules are satisfied, the multi-transport parameters, or no incoming fields receiving filter out
supplement:
render the locals (), the name space will be located all the names are passed to the template page
forms component rendering Tags:
forms rendering component can only help you get user input (input boxes, selection boxes, drop-down box ..) Tags
forms a front end assembly canceled verification function is added in the form tag novalidata
Background check data forms assembly
Use directly
form_obj = class MyForm () # instantiated field
if request.method == 'POST': # determines whether he is a post request
form_obj = MyForm (request.POST) # direct the incoming class request.POST dictionary, because request.post itself is a dictionary, and forms the components required parameter is the dictionary. So the front desk directly pass over the data pass in judgment. Results obtained with is_valid () as bool value is obtained, is determined directly
form components ended input current information is not legal when the data on the page does not refresh the reservation, so convenient user modify
Common forms component parameters:
username = forms.CharField (max_length = 6, label = 'username', error_messages = { 'max_length': 'username longest position 6',
'Required': 'a user name can not be empty'})
1. max_length: set the maximum length
2. label = 'user name': custom field names, field names because the default is to create, in English, can be customized to Chinese
3. initial = 'Enter Username': input box setting default values
4. Normal # field error information setting error_messages = { 'max_length': 'username maximum only six,' required ':' a user name can not be empty '}: error parameter is to be a write to a key dictionary. value can be easily set
The error field is provided Mailbox # error_messages = { 'invalid': 'mailbox format is not correct', 'required': 'E-mail can not be empty'}: Error message
6. widget = widgets.PasswordInput (attrs = { 'class': 'form-control'}): set a password for cipher text
Field validation forms of two ways
1. RegexValidator validator
2. hook function
Hook function is written in the following field class function to check the client information transmission over
1. Local hooks. Field name for the function to do the check (the beginning of the function name is clean)
For example def clean_username (self): Analyzing plus
username = clenaed_data.get('username')