When the page is loaded, it will automatically initiate a query for province information to the backend and insert the result into the province drop-down list
When the province information is changed, the province id is sent to the backend to initiate a city/state query through ajax, and the result is inserted into the city/state drop-down list
When the city and state information is changed, the city id will be sent to the backend to initiate a district/county query through ajax, and the result will be inserted into the district/county drop-down list
So as to realize the three-level linkage of provinces, cities and counties
Fourth, get the list of provinces
1. Front-end implementation
When the page is loaded, automatically initiate a province query to the backend
2. Interface design and definition
2.1 Request method:
Options
Program
Request method
get
Request address
/areas/
2.2 Request parameters:
parameter name
Types of
Must pass
Description
area_id
string
no
air
2.3 Response result: json
Response result
Response content
code
status code
errmsg
Error message
province_list
List, each element is a dictionary: {"id":province id, "name":province name}
If the parameter is not empty, it means to query city or county information
Get the city information with id=city id through the Areas class
Obtain the county data set with parent__id=city id through the Areas class
Read the data set cyclically to generate a list of county information
County information dictionary: {"id":county id,"name":county name}
Generate subs_data data
Return front-end json data
Code and city code reuse
Seven, code optimization
The data of provinces, cities and counties will basically not change, and can be stored in the cache to increase the number of mysql data reads and improve the response speed
This time saved in the default area of CACHES
Exception capture and handling
Parameterize the expiration time of province, city and county data
1. Description
Get parameters
Determine whether the parameter is empty
If the parameter is empty, get the province information
If the parameter is not empty, get the city and county information according to area_id
Get province information:
Read province_model_list from CACHES
If the province_model_list is empty, the table request has no province information or the province information is invalid, and it needs to be read from the mysql database
Due to the need to access the database, there may be read failures or abnormal errors, and exception capture is required.
Through the Areas class, get the data set whose parent is empty
Read the data set cyclically to generate a list of province information
Save province information in CACHES
No need to return here, because province_model_list exists, it also needs to be returned
If an exception occurs, save the log and return JSON with error information
Return JSON containing province information
Get city and county information
Read sub_data+area_id information from CACHES
If the sub_data+area_id information is empty, the table request has no city or county information or the city and county information is invalid, and it needs to be read from the mysql database
Due to the need to access the database, there may be read failures or abnormal errors, and exception capture is required.
Get the province information with id=province id through the Areas class
Get the city data set with parent__id=province id through the Areas class
Read the data set cyclically to generate a list of province information
Generate subs_data data
Save the city and county information in CACHES: sub_data+area_id
If an exception occurs, save the log and return JSON with error information