(I realize many of you may not have HTMX experience, but hopefully somebody can help me out here)
I’ve been experimenting with HTMX, but now unfortunately am stuck while trying to implement AJAX functionality into my already working Django web app.
The situation is this: I have a many-to-many relationship between my Bet and Tag models. I’m trying to use a button (wrapped inside a POST form) to send a POST request to the URL of a function based view that removes the many-to-many relationship between particular instances of the models. However, for some reason it seems that the function based view is never called upon.
I’m only just starting to use HTMX so I’m pretty confused on what could be wrong. I had a simpler project with HTMX where I got a similar case to work just fine.
The view:
def remove_bet_tag(request):
print("this is working!!")
bet_id = request.POST.get("bet-id")
removed_tag = Tag.objects.get(id=request.POST.get('tag-id'))
removed_tag.associated_bets.remove(Bet.objects.get(id=bet_id))
return render(request, 'tags/partials/bet_tag_list.html')
(the print statement never is actually rendered to the terminal)
URL patterns:
from django.urls import path
from .views import TagListView, NewTagView, UpdateTagView, DeleteTagView, BetTagPageView, BetNewTagView, remove_bet_tag
urlpatterns = [
path("", TagListView.as_view(), name="tag_list"),
path("new_tag/", NewTagView.as_view(), name="new_tag"),
path("update_tag/<uuid:pk>/", UpdateTagView.as_view(), name="update_tag"),
path("delete_tag/<uuid:pk>/", DeleteTagView.as_view(), name="delete_tag"),
path("bet_tag_page/<uuid:pk>/", BetTagPageView.as_view(), name="bet_tag_page"),
path("bet_new_tag/", BetNewTagView.as_view(), name="bet_new_tag"),
]
htmx_urlpatterns = [
path("remove_bet_tag/<uuid:pk>/", remove_bet_tag, name="remove_bet_tag"),
]
urlpatterns += urlpatterns + htmx_urlpatterns
The template containing the form:
{% for tag in bet.tags.all %}
<tr>
<td><b>{{ tag }}</b></td>
<td><i>{{ tag.description }}</i></td>
<td>
<form action="{% url 'bet_tag_page' pk=bet.pk %}" style="display:inline;">
{% csrf_token %}
<input type="hidden" name="tag-id" value="{{tag.id}}">
<input type="hidden" name="bet-id" value="{{bet.pk}}">
<button hx-post="{% url "remove_bet_tag" bet.pk %}"
hx-target="#bet_tag_list"
class="btn btn-danger border border-dark"
type="submit"
name="remove-tag">
Remove Tag
</button>
</form>
</td>
</tr>
{% endfor %}