【问题标题】:How to filter list data by id and show the single object in HTML template by django?如何按 id 过滤列表数据并通过 django 在 HTML 模板中显示单个对象?
【发布时间】:2021-10-06 03:01:39
【问题描述】:

这是下面的 Json 列表:

"chartOfAccounts": [
            {
                "id": 147,
                "name": "Sales Product - Wholesale test",
                "description": "Sales - Wholesale",
                "balance": "0.00",
                "is_active": true,
                "is_editable": true,
                "account_detail_type": {
                    "id": 5,
                    "name": "Accounts Receivable (A/R)",
                    "account_type": {
                        "id": 2,
                        "name": "Accounts Receivable (A/R)",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 146,
                "name": "Sales Product - Wholesale test",
                "description": "Sales - Wholesale",
                "balance": "0.00",
                "is_active": true,
                "is_editable": true,
                "account_detail_type": {
                    "id": 5,
                    "name": "Accounts Receivable (A/R)",
                    "account_type": {
                        "id": 2,
                        "name": "Accounts Receivable (A/R)",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 145,
                "name": "Cash in hand rony",
                "description": "Cash in hand rony",
                "balance": "-45980.00",
                "is_active": true,
                "is_editable": true,
                "account_detail_type": {
                    "id": 1,
                    "name": "Cash and cash equivalents",
                    "account_type": {
                        "id": 1,
                        "name": "Cash and cash equivalents",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 144,
                "name": "6yt4",
                "description": "gyyy",
                "balance": "5203.00",
                "is_active": true,
                "is_editable": true,
                "account_detail_type": {
                    "id": 1,
                    "name": "Cash and cash equivalents",
                    "account_type": {
                        "id": 1,
                        "name": "Cash and cash equivalents",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 99,
                "name": "Cash in hand Monim",
                "description": "monim cash",
                "balance": "-1759.00",
                "is_active": true,
                "is_editable": true,
                "account_detail_type": {
                    "id": 1,
                    "name": "Cash and cash equivalents",
                    "account_type": {
                        "id": 1,
                        "name": "Cash and cash equivalents",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 98,
                "name": "Monim Capital",
                "description": "Monim Capital",
                "balance": "50000.00",
                "is_active": true,
                "is_editable": true,
                "account_detail_type": {
                    "id": 10,
                    "name": "Owen's Capital",
                    "account_type": {
                        "id": 6,
                        "name": "Owen's Capital",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "cr"
                }
            },
            {
                "id": 93,
                "name": "Payroll - m@m.bn",
                "description": "Payroll - m@m.bn",
                "balance": "0.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 12,
                    "name": "Expenses",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 12,
                "name": "Profit",
                "description": "Profit",
                "balance": "437690.75",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 11,
                    "name": "Income",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "cr"
                }
            },
            {
                "id": 10,
                "name": "Rajib",
                "description": "Test",
                "balance": "50000.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 11,
                    "name": "Income",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "cr"
                }
            },
            {
                "id": 9,
                "name": "Sales - Product",
                "description": "Sales - Product",
                "balance": "0.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 11,
                    "name": "Income",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "cr"
                }
            },
            {
                "id": 8,
                "name": "Purchases - Product",
                "description": "Purchases - Product",
                "balance": "47388.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 12,
                    "name": "Expenses",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 7,
                "name": "Payroll Expenses",
                "description": "Payroll Expenses",
                "balance": "0.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 12,
                    "name": "Expenses",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 6,
                "name": "Office expenses",
                "description": "Office expenses",
                "balance": "28899.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 12,
                    "name": "Expenses",
                    "account_type": {
                        "id": 7,
                        "name": "Revenue",
                        "principle": {
                            "id": 3,
                            "name": "Owen's Equity",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 5,
                "name": "Accounts Payable (A/P)",
                "description": "Accounts Payable (A/P)",
                "balance": "18491.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 9,
                    "name": "Accounts Payable (A/P)",
                    "account_type": {
                        "id": 5,
                        "name": "Current liabilities",
                        "principle": {
                            "id": 2,
                            "name": "Liability",
                            "calculation_type": "cr"
                        }
                    },
                    "calculation_type": "cr"
                }
            },
            {
                "id": 4,
                "name": "Inventory",
                "description": "Inventory",
                "balance": "88682.75",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 7,
                    "name": "Inventory",
                    "account_type": {
                        "id": 3,
                        "name": "Current assets",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 3,
                "name": "Accounts Receivable (A/R)",
                "description": "Accounts Receivable (A/R)",
                "balance": "2500.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 5,
                    "name": "Accounts Receivable (A/R)",
                    "account_type": {
                        "id": 2,
                        "name": "Accounts Receivable (A/R)",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            },
            {
                "id": 1,
                "name": "Rajib",
                "description": "Test",
                "balance": "431248.00",
                "is_active": true,
                "is_editable": false,
                "account_detail_type": {
                    "id": 1,
                    "name": "Cash and cash equivalents",
                    "account_type": {
                        "id": 1,
                        "name": "Cash and cash equivalents",
                        "principle": {
                            "id": 1,
                            "name": "Asset",
                            "calculation_type": "dr"
                        }
                    },
                    "calculation_type": "dr"
                }
            }
        ],

我想从这个 JSON 列表数据中按 id 过滤它并在 html 模板上显示它!基本上这需要编辑任务。当有人点击编辑按钮时,特定的 id 被传递。 like.. 当 id 为 147 时,我想打印 147 的其他数据,如姓名、描述、余额、帐户详细信息类型!这就像一个单独的对象。

查看以下 HTML 格式:这里我想在值 {{}} 模板中显示名称、描述、余额等数据:

                                 <div class="form-group row">
                                    <label class="col-form-label col-md-2">Name</label>
                                    <div class="col-md-10">
                                        <input type="text" class="form-control" name="name"
                                               value="{{ }}">
                                    </div>
                                </div>
                               <div class="form-group row">
                                    <label class="col-form-label col-md-2">Description</label>
                                    <div class="col-md-10">
                                        <input type="text" class="form-control" name="desc"
                                               value="{{ }}">
                                    </div>
                                </div>
                              <div class="form-group row">
                                    <label class="col-form-label col-md-2">Balance</label>
                                    <div class="col-md-10">
                                        <input type="text" class="form-control" name="bal"
                                               value="{{ }}">
                                    </div>
                                </div>

【问题讨论】:

  • 您可以为此使用 javascript,例如编辑按钮上的 onClick 事件

标签: python django oop web rest


【解决方案1】:

最好将此json文件作为模型存储在项目中,并在发送所需的Id时渲染html。 例如,如果 givenId = 147 被按下。 view.py 中的函数可以在哪里运行

chart = chartsOfAccounts.object.filter(id=givenId)
return render(request,<htmlfile>,{"chart":chart})

在html中你可以使用图表模型来获取以下数据。

{{ 图表名称 }} {{ chart.desc }}

否则将json文件转换为字典并发送值

【讨论】:

  • AttributeError at 'list' object has no attribute 'objects'
  • 您需要在model.py中创建模型,将数据存储在数据库中,然后将数据作为对象检索。它不应该是列表类型
  • 我知道……伙计!其实问题是..没有模型!我正在通过 request.post() 方法从 API 获取 JSON 数据。但最后我用自己的努力解决了这个问题!感谢您提出一些可能的解决方案!
猜你喜欢
  • 1970-01-01
  • 2019-04-05
  • 2013-09-17
  • 2020-06-13
  • 2014-08-22
  • 2018-04-22
  • 2021-04-29
  • 2017-03-29
  • 2015-03-24
相关资源
最近更新 更多