HTMX/Django: Почему hx-post="." отправляет сообщение в неправильную конечную точку?
Я пытаюсь получить очень простой пример CRUD, работающий через htmx и django, добавляя простую заметку/комментарий к закладке.
Я не знаю, почему моя кнопка отправки идет к неправильной конечной точке, и я надеюсь, что вы можете поделиться светом в этом вопросе. Я прикрепил несколько вопросов к нижней части этого вопроса.
Неправильно: HTTP POST /notebook/ 405 [0.12, 127.0.0.1:50193]
Разыскиваются: HTTP POST /b/<int:pk>/add-note/
Urls:
path('b/<int:pk>/add-note/', add_bookmark_note, name="add-bookmark-note")
Вид:
@login_required
def add_bookmark_note(request, pk):
if request.method == "POST":
bookmark = get_object_or_404(Bookmark, pk)
note = request.POST.get('bookmark_note')
if note:
old_note = Comment.objects.filter(user=request.user, bookmark=bookmark).first()
if old_note:
old_note.comment = note
old_note.save()
print("updated")
else:
Comment.objects.create(comment=note, user=request.user, bookmark_id=pk)
print("created")
return render(request, "bookmark/htmx/bookmark_form.html", context={
"form": CommentForm
})
Форма
<div hx-target="this" hx-swap="outerHTML" class="">
<form method="POST">
{% csrf_token %}
<textarea name="bookmark_note" class="form-control" id="floatingTextarea2" style="height: 100px">{% if bookmark.comment_set.all.0.comment %}{{ bookmark.comment_set.all.0.comment }}{% endif %}</textarea>
<label class="form-text" for="floatingTextarea2">{% trans "Write some notes" %}</label>
<button type="submit" hx-post=".">Submit</button>
</form>
</div>
Шаблон
<div class="card">
<div class="card-body">
<div hx-target="this">
<p>{{ bookmark.comments|linebreaks }}</p>
<button hx-get="{% url 'add-bookmark-note' bookmark.id %}" hx-swap="outerHTML">Add note</button>
</div>
</div>
</div>
Вопросы:
- Why is hx-post pointing to the wrong endpoint?
- Why am I getting a
NoReverseMatch
if I use the url name:<button type="submit" hx-post="{% url 'add-bookmark-note' bookmark.id %}">Submit</button>
- How would I fix this?