我正在尝试在DB中保存Json数据,然后将其显示在模板中。
数据保存为:
{"reaction": [{"reactionmeddrapt": "Stress fracture", "reactionmeddraversionpt": "20.1", "reactionoutcome": "1"}, {"reactionmeddrapt": "Drug effect incomplete", "reactionmeddraversionpt": "20.1", "reactionoutcome": "6"}, {"reactionmeddrapt": "Osteoarthritis", "reactionmeddraversionpt": "20.1", "reactionoutcome": "3"}, {"reactionmeddrapt": "Fracture delayed union", "reactionmeddraversionpt": "20.1", "reactionoutcome": "6"}, {"reactionmeddrapt": "Knee arthroplasty", "reactionmeddraversionpt": "20.1", "reactionoutcome": "6"}], "drug": [{"medicinalproduct": "PROLIA", "drugindication": "PRODUCT USED FOR UNKNOWN INDICATION", "drugadministrationroute": "065", "drugdosagetext": "UNK, Q6MO", "openfda": {"manufacturer_name": ["Amgen Inc"], "unii": ["4EQZ6YO2HI"], "product_type": ["HUMAN PRESCRIPTION DRUG"], "rxcui": ["993452", "993456"], "spl_set_id": ["49e5afe9-a0c7-40c4-af9f-f287a80c5c88"], "route": ["SUBCUTANEOUS"], "generic_name": ["DENOSUMAB"], "brand_name": ["PROLIA"], "product_ndc": ["55513-710"], "pharm_class_epc": ["RANK Ligand Inhibitor [EPC]"], "substance_name": ["DENOSUMAB"], "spl_id": ["f0a4aa32-b689-4a2a-b575-7b3156e79c0c"], "pharm_class_moa": ["RANK Ligand Blocking Activity [MoA]"], "application_number": ["BLA125320"], "nui": ["N0000187054", "N0000187055"], "package_ndc": ["55513-710-01"]}, "drugdosageform": "SOLUTION FOR INJECTION", "drugcharacterization": "1", "activesubstance": {"activesubstancename": "DENOSUMAB"}, "drugauthorizationnumb": "125320", "actiondrug": "5", "drugadditional": "3"}]}
在我看来,我使用json.loads(json_data)
现在我试图在模板中迭代这个但不能这样做。
我已经尝试使用json_data和json.loads而没有json.loads。它似乎不起作用。
我已经尝试了模板{% for key, value......
中的所有内容以及{{json_data.field_name}}
这似乎是关于SO的最常见问题之一,到目前为止还没有具体答案。
答案 0 :(得分:1)
如果字典的格式是固定的,为什么不分别循环药物和反应并手动打印模板中的东西?
您可以根据需要编辑HTML。如果其中一些项目是列表,您也可以循环遍历它们。例如,我循环浏览以下代码段中的drug.openfda.rxcui
。
{% for drug in json_data.drug %}
<p>Actiondrug: {{ drug.actiondrug }}</p>
<p>Activesubstance: {{ drug.activesubstance.activesubstancename }}</p>
<p>Drugadditional: {{ drug.drugadditional }}</p>
<p>Drugadministrationroute: {{ drug.drugadministrationroute }}</p>
<p>Drugauthorizationnumb: {{ drug.drugauthorizationnumb }}</p>
<p>Drugcharacterization: {{ drug.drugcharacterization }}</p>
<p>Drugdosageform: {{ drug.drugdosageform }}</p>
<p>Drugdosagetext: {{ drug.drugdosagetext }}</p>
<p>Drugindication: {{ drug.drugindication }}</p>
<p>Medicinalproduct: {{ drug.medicinalproduct }}</p>
<p>OpenFDA</p>
<p>application_number: {{ drug.openfda.application_number }}</p>
<p>brand_name: {{ drug.openfda.brand_name }}</p>
<p>generic_name: {{ drug.openfda.generic_name }}</p>
<p>manufacturer_name: {{ drug.openfda.manufacturer_name }}</p>
<p>nui: {{ drug.openfda.nui }}</p>
<p>package_ndc: {{ drug.openfda.package_ndc }}</p>
<p>pharm_class_epc: {{ drug.openfda.pharm_class_epc }}</p>
<p>pharm_class_moa: {{ drug.openfda.pharm_class_moa }}</p>
<p>product_ndc: {{ drug.openfda.product_ndc }}</p>
<p>product_type: {{ drug.openfda.product_type }}</p>
<p>route: {{ drug.openfda.route }}</p>
{# example of looping through a list #}
<p>rxcui: {% for r in drug.openfda.rxcui %}{{ r }}{% endfor %}</p>
<p>spl_id: {{ drug.openfda.spl_id }}</p>
<p>spl_set_id: {{ drug.openfda.spl_set_id }}</p>
<p>substance_name: {{ drug.openfda.substance_name }}</p>
<p>unii: {{ drug.openfda.unii }}</p>
{% endfor %}
{% for reaction in json_data.reaction %}
{% for key, val in reaction.items %}
<p>reactionmeddrapt: {{ reaction.reactionmeddrapt }}</p>
<p>reactionmeddraversionpt: {{ reaction.reactionmeddraversionpt }}</p>
<p>reactionoutcome: {{ reaction.reactionoutcome }}</p>
{% endfor %}
{% endfor %}