blue-gold-89534
12/24/2021, 2:03 AM<tbody id="table">
{% for product in page.object_list %}
<tr hx-target="this">
<td>
{{ product.number }}
</td>
{% for key, value in product.data.items %}
<td>{{ value }}</td>
{% endfor %}
<td>
<button class="btn btn-primary btn-sm"
hx-get="{% url 'detail' product.pk %}"
hx-swap="outerHTML">
edit
</button>
</td>
</tr>
{% endfor %}
</tbody>
clicking the button switches to inline editing with a form (works) - but I am not sure on how to revert back. This is the HTML that gets swapped in:
<tr id="productrow">
{% for field in form %}
<td>
{{ field.errors }}
{{ field }}
</td>
{% endfor %}
<td>
<button
type="submit"
class="btn btn-success"
hx-post="{% url 'update' 4 %}"
hx-target="#productrow"
hx-swap="outerHTML">
OK
</button>
<button
class="btn btn-danger"
hx-target="#productrow"
hx-swap="outerHTML">
CANCEL
</button>
</td>
</tr>
How would the OK and the CANCEL Button be ablee to switch back to the new/original content?mysterious-toddler-20573
12/24/2021, 2:46 AMhx-get
on the cancel that would re-render the display row, somewhat like this example: https://htmx.org/examples/click-to-edit/blue-gold-89534
12/25/2021, 9:44 AMhtml
<button class="btn btn-danger"
hx-target="#productrow"
hx-post="{% url 'update' pk %}"
hx-swap="click[testing]"
hx-swap="outerHTML">
CANCEL
</button>
{% block js %}
<script>
document.body.addEventListener('htmx:configRequest', (event) => {
event.detail.headers['X-CSRFToken'] = '{{ csrf_token }}';
});
</script>
<script>
var testing = function() {
console.log("hello");
return true
}
</script>
{% endblock js %}
But this never prints something - why is the function never called?blue-gold-89534
12/26/2021, 9:33 AMhtml
<form enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
{{ field }}
{% endfor %}
<button type="submit"
class="btn btn-success"
hx-post="{% url 'update' pk %}"
hx-target="#productrow"
hx-swap="outerHTML">
submit
</button>
</form>
this correctly sends a request to my django endpoint on update
. My problem is, that I have file fields in this form, so I needed to add a method="post"
and if I do so, the request gets sent to the current view (being called HTMXProducts
). My `urls.py`:
python
urlpatterns = [
path("products/", views.HTMXProducts.as_view(), name = 'products'), # <--- hx-post is ignored and post is sent here when method="post" is added to form
path('detail/<int:pk>', views.ProductDetailView.as_view(), name = 'detail'),
path('update/<int:pk>', views.ProductUpdateView.as_view(), name = 'update'), # <--- ends up (correctly) here without method="post"
]
what am I missing here?abundant-breakfast-94967
12/26/2021, 2:32 PMabundant-breakfast-94967
12/26/2021, 2:32 PMabundant-breakfast-94967
12/26/2021, 2:32 PMblue-gold-89534
12/26/2021, 2:35 PMfieldvalue:"[object File]"
blue-gold-89534
12/26/2021, 2:35 PMpost
and now that you mention it, hx-post
probably takes care of this already.abundant-breakfast-94967
12/26/2021, 2:36 PMblue-gold-89534
12/26/2021, 2:36 PMblue-gold-89534
12/26/2021, 2:37 PMblue-gold-89534
12/26/2021, 2:37 PMblue-gold-89534
12/26/2021, 2:47 PMblue-gold-89534
12/26/2021, 2:52 PMclean
method of the form I want to work with this file, but it looks like the enctype is wrong: <form enctype="multipart/form-data">
mysterious-toddler-20573
12/26/2021, 3:03 PMmysterious-toddler-20573
12/26/2021, 3:03 PMlimited-pillow-24427
12/26/2021, 8:35 PMabundant-breakfast-94967
12/26/2021, 8:40 PMbase.html
that has all of my scripts, css, etc as well as my nav. And then I have home.html
which extends base, as well as a couple of other nav options, i.e. search.html
that also extend base. Is there a good way to prevent the page from reloading when I navigate from home to another one of the nav options?
I could hx-swap body, but the problem if someone goes to /app/
(home) or /app/search
(search) in their browser, I need base.html
which both extends to load at least onceblue-gold-89534
12/26/2021, 10:39 PMblue-gold-89534
12/26/2021, 10:42 PMprehistoric-cat-63987
12/27/2021, 10:56 AMprehistoric-cat-63987
12/27/2021, 10:57 AMprehistoric-cat-63987
12/27/2021, 10:58 AMgorgeous-airport-54386
12/27/2021, 11:06 AMblue-gold-89534
12/27/2021, 2:24 PMhtml
<button hx-get="{% url 'detail' product.pk %}"
hx-trigger="click[testing]"
hx-swap="outerHTML">
test
</button>
and a JS function
JS
var testing = function () {console.log("test was clkd")}
blue-gold-89534
12/27/2021, 2:25 PMblue-gold-89534
12/27/2021, 2:25 PMblue-gold-89534
12/27/2021, 2:26 PM