Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Django Web dynamic three linkage     - Basic Operation Linux files and directories (Linux)

- Hadoop 2.0 Detailed Configuration Tutorial (Server)

- System-level alias vs Oracle ADR functionality (Database)

- Python control multi-process and multi-threaded concurrency (Programming)

- ThinkPad X201i installation notes Debian 7.5 (Linux)

- EXP-00091: Exporting questionable statistics Processing Method (Database)

- Android main thread message system (Handler Looper) (Linux)

- Oracle 11g Export guide problem not an empty table (Database)

- Paint with Vim (Linux)

- Oracle 11G using DG Broker create DataGuard (Database)

- Object Oriented Programming Java reflection (Programming)

- 5 steps to help you become a good Docker contributors (Linux)

- open V switch port mirror in OpenStack neutron (Server)

- Linux Troubleshooting: How to save the status of the SSH session is closed (Linux)

- Debian (Wheezy) Install Java environment / replace OpenJDK as the SUN JDK (Linux)

- OpenGL Superb Learning Notes - GLSL language foundation (Programming)

- Ubuntu Server 14.04 installation Web server (Linux + Apache + MySQL + PHP) (Server)

- Let MySQL 5.6 support Emoji expression (Database)

- Debian GNU / Linux service list acquisition, shutting down services or run (Linux)

- Bash Getopts - let your script supports command line parameters (Linux)

 
         
  Django Web dynamic three linkage
     
  Add Date : 2018-11-21      
         
         
         
  Our demand is hoped the data stored on the server, then you can request data to the server each time a user clicks on the need to achieve a similar following logic functions:

Users click -> onchange trigger event -> request data to the server -> server returns data -> add data to the current page

Here with the Python Django to achieve this function.

(PS: Django platform about how to deploy and set up, there is not much to say, we can look at "Django Book", I had to find a lot of information on the Internet, users write or some other books, are not "Django Book "I speak meticulous and thorough, it is strongly recommended!)

-------------------------------------------------- ------------------------------

1. Platform Environment

Operating System: Windows 7 64 Wei

Development Environment: Eclipse PyDev

-------------------------------------------------- ------------------------------

2. The folder structure

When you create a project or when Django app, many files are automatically generated, the following will be able to talk about the lingua franca of the file.

-------------------------------------------------- ------------------------------

3. The main function of the code file

(1) MapPro / settings.py
    The key is used to set the statics and templates, to tell the presence of these two paths Django directories, add the code as follows:
TEMPLATE_DIRS = (
                os.path.join (BASE_DIR, 'templates'),
                )
  
STATICFILES_DIRS = (
                    os.path.join (BASE_DIR, 'statics'),
                    )

(2) MapPro / urls.py
The mapping between the path and the views url view function, the entire code is as follows:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from app01 import views
  
urlpatterns = patterns ( '',
    # Examples:
    # Url (r '^ $', 'MapPro.views.home', name = 'home'),
    # Url (r '^ blog /', include ( 'blog.urls')),
  
    url (r '^ admin /', include (admin.site.urls)),
    url (r '^ map / $', views.Map),
    (R '^ GetCityData / $', views.Return_City_Data),
    (R '^ GetCountryData / $', views.Return_Country_Data),
)

(3) app01 / views.py
    View function file to handle business logic, all the code is as follows:
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.core.context_processors import request
import json
  
# Create your views here.
  
def Map (request):
    return render_to_response ( "map.html")
    #return HttpResponse ( "Hello!")
      
Place_dict = {
        "GuangDong": {
                        "GuangZhou": [ "PanYu", "HuangPu", "TianHe"],
                        "QingYuan": [ "QingCheng", "YingDe", "LianShan"],
                        "FoShan": [ "NanHai", "ShunDe", "SanShui"]
                        },
        "ShanDong": {
                        "JiNan": [ "LiXia", "ShiZhong", "TianQiao"],
                        "QingDao": [ "ShiNan", "HuangDao", "JiaoZhou"]
                        },
        "HuNan": {
                        "ChangSha": [ "KaiFu", "YuHua", "WangCheng"],
                        "ChenZhou": [ "BeiHu", "SuXian", "YongXian"]
                    }
    };
def Return_City_Data (request):
    province = request.GET [ 'Province']
    print province
    City_list = []
    for city in Place_dict [province]:
        City_list.append (city)
    return HttpResponse (json.dumps (City_list))
      
def Return_Country_Data (request):
    province, city = request.GET [ 'Province'], request.GET [ 'City']
    print province, city
    Country_list = Place_dict [province] [city]
    return HttpResponse (json.dumps (Country_list))

    The code here is relatively simple, do not write a comment.

(4) templates / map.html
    templates directory is mainly used to place the template file page, and here I only created map.html file, as follows: