因此,当使用 HTMX 将表单提交到我的 Django 后端时,我试图显示一条反馈消息,说“已添加”或“失败”。
基本上我现在拥有的是一个执行 hx-post 的表单,回复是一个div
包含更新信息的表单,它与div
.
<div id="list">
<!-- The stuff in here is loaded through another template, which is
the result of a successful myApp:add operation. -->
</div>
<form hx-post="{% url 'myApp:add' eid=e.id %}" hx-swap="outerHTML" hx-target="#list">
<!-- Some stuff in here -->
</form>
<div id="result">
<!-- Here I should print a message saying correct or incorrect
depending on the HTMX result (if the entry was added or there was
any kind of error, even connection errors) -->
</div>
问题是,如果表单或请求本身出现错误,列表将保持不变,但我希望它在result
div 中打印类似“错误”的内容。如果正确添加了新条目,我想在结果 div 中打印“成功”消息。
请注意,我不能将结果 div 作为 hx-post 响应 DOM 的一部分返回,因为连接可能会失败。所以不会显示失败消息。
如果有帮助的话,我也在使用 Alpine.js。