Django Python type directly in the data to render a template language into JQuery types of data

1, provided that:

  When required by a sequence of data types and Python JQuery data type conversion, the following we will be done through the JSON serialized,

  1: In Python Python type environment each data type conversion Json:

    Python ----> Json :( Note that import json) json.dumps (data), data type data for Python

    Json ----> Python: json.loads (data), data strings of Json

  2: In the environment JQuery jQuery Json data type Data type conversion another:

    ---- Jquery> Json: the JSON .stringify (Data), Data is the data type Jquery

    ---- Json> Jquery: the JSON.parse (Data), Data string to Json

2, Django rendering data

COMMODITY_TYPE = {
            "Women / underwear": [ "popular seasonal", "featured topless", "romantic dresses", "Featured Women"],
            "Men / outdoor sports": [ "trend of men", "men's jacket", "men inside the ride", "features men"],
            "Women's / men's shoes / bags": [ "recommendation shoes", "shoes tide", "men's fashion", "trend handbags", "functional bags", "fine men"],
            "Household appliances": [ "TV", "air conditioning", "Kitchen big power", "small kitchen electric", "electrical life", "Health &"],
            "Mobile phones / computers Office": [ "hot phone", "smart digital", "Computers", "mobile phone deals"],
            "Beauty / Personal Care": [ "facial skin care," "Perfume & Cosmetics", "hair care", "oral care"],
            "Books / entertainment / education / e-book": [ "supplementary materials", "Humanities and Social Sciences", "education and training", "administered by the inspirational"],
            "Home / Furniture / Decoration / kitchen": [ "boutique sofa", "bedroom furniture", "kitchen utensils", "whole-house custom"],
            "Snacks / tea and wine / food imports": [ "imported food", "snack", "high spirits", "hardcover red wine", "quality tea"],
            "Automotive / Accessories / Supplies": [ "cushion mat", "beauty cleansing", "car decoration", "Electric Car", "oil tire"],
            "Textile / Furnishings / Flowers": [ "What's Hot", "bedding", "home accessories", "home cloth", "flowers and plants"],
        }
def commodityManage(request):
    import json
    return render(request, "shop/commodityManage.html", {"commodityClass":json.dumps(COMMODITY_TYPE)})

 3, the template language specified rendering data location

  Description: data type is converted to jQuery, js script should specify the location and define a variable to store the received data, in the following code {{commodityClass | safe}} Json string to be rendered need in the HTML code using 'single quotes surround, so that the JSON.parse () to act, simultaneously, it requires the use of filters specified code security safe, to ensure Json string data code is not escaped before transmission.

<script>
        var commodityClass = JSON.parse('{{ commodityClass|safe }}');
</script>

 

Guess you like

Origin www.cnblogs.com/yuncaige/p/12514280.html