【发布时间】:2022-01-24 14:32:43
【问题描述】:
我有一个将一些数据发布到 Django 后端并返回 JSON 响应的页面。我在使用模板解析它时遇到问题。 任何帮助,将不胜感激。提前谢谢你。
<div class="card-body">
<form>
{% csrf_token %}
<div class="input-group mb-3">
<label class="input-group-text">Command</label>
<select class="form-select" name="run_show_command" id="run_show_command">
<option selected disabled>Choose...</option>
<option value="{{ data.version.id }}:1">show ip interface brief</option>
<option value="{{ data.version.id }}:2">show interfaces description</option>
<option value="{{ data.version.id }}:3">show ip arp</option>
<option value="{{ data.version.id }}:4">show ip route</option>
<option value="{{ data.version.id }}:5">show ip cef</option>
<option value="{{ data.version.id }}:6">show version</option>
</select>
<button type="submit" class="btn btn-sm btn-success" hx-post="{% url 'inventory:device_run_command' %}" hx-target="#command_output" mustache-template="m_template" hx-indicator="#loader_bars">
<i class="fas fa-terminal"></i> Run
</button>
</div>
</form>
<div class="d-flex justify-content-center" hx-ext="client-side-templates" id="command_output">
<img id="loader_bars" class="htmx-indicator" alt="Loading..." src="{% static 'images/loaders/bars.svg' %}"/>
<template id="m_template" type="text/mustache">
{% for data in dataset %}
{% for key, value in data.items %}
<li>{{ key }} {{ value }}</li>
{% endfor %}
{% endfor %}
</template>
</div>
</div>
JSON:
[{ "intf": "GigabitEthernet1", "ipaddr": "10.10.20.48", "status": "up", "proto": "up" }, { "intf": "GigabitEthernet2", "ipaddr": "unassigned", "status": "administratively down", "proto": "down" }, { "intf": "GigabitEthernet3", "ipaddr": "unassigned", "status": "administratively down", "proto": "down" }, { "intf": "Loopback1", "ipaddr": "10.10.10.100", "status": "up", "proto": "up" }, { "intf": "Loopback123", "ipaddr": "unassigned", "status": "up", "proto": "up" }, { "intf": "Loopback510", "ipaddr": "50.10.10.10", "status": "up", "proto": "up" }, { "intf": "Loopback511", "ipaddr": "51.10.10.10", "status": "up", "proto": "up" }, { "intf": "Loopback512", "ipaddr": "52.10.10.10", "status": "up", "proto": "up" }, { "intf": "Loopback666", "ipaddr": "6.6.6.6", "status": "up", "proto": "up" } ]
【问题讨论】:
-
具体是什么不起作用?你有错误吗?
-
不,我希望根据模板解析输出,但它是原始渲染的(如 JSON 示例中所示)。
-
仅查看 HTMX 文档中的示例,我认为您需要在 HTML 中将
hx-ext="client-side-templates"向上移动,例如在<div class="card-body">元素上。 -
我会试一试,但到目前为止我已经尝试了所有方法,但没有任何效果,将再试一次:)
-
好的,所以我把它移到了上面的 DIV(卡和卡体),然后根本没有显示输出:/