I’m trying to implement inline editing feature using HTMX in Django form. I’ve checked the url routing several times and am pretty sure, that everything should be correct.
I’m gettings django.urls.exceptions.NoReverseMatch: Reverse for 'update_work_hours' with arguments '('',)' not found. 1 pattern(s) tried: ['nuomotojo\-platforma/darbo\-laikas/atnaujinti/(?P<pk>[0-9]+)/\Z']
error while executing get request.
Template (delete works perfectly fine, so I assume, that hours.pk is passed correctly):
<div id="work_hour_day" class="flex flex-col md:flex-row items-center my-2">
<span>{{ hours.get_day_of_week_display }}: {{ hours.opening_time }}-{{ hours.closing_time }}</span>
<div class="flex gap-2 mx-2">
<button hx-get="{% url 'rentals:update_work_hours' hours.pk %}" hx-target="closest #work_hour_day" hx-swap="outerHTML" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Keisti</button>
<button hx-delete="{% url 'rentals:delete_work_hours' hours.pk %}" hx-target="closest #work_hour_day" hx-swap="outerHTML swap:1s" hx-trigger='confirmed' onClick="Swal.fire({title: 'Confirm', text: 'Do you want to continue?', icon: 'question'}).then((result)=>{
if(result.isConfirmed){
htmx.trigger(this, 'confirmed');
}
})" class="focus:outline-none text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900" type="button">Ištrinti</button>
</div>
</div>
Views.py:
class UpdateRentalWorkingHours(View):
def get(self, request, pk):
working_hours = get_object_or_404(RentalWorkingHours, pk=pk)
form = UpdateWorkingHoursForm(instance=working_hours)
day_of_week = working_hours.get_day_of_week_display()
# If request from cancel button in the form, return the initial data
trigger = request.headers.get("HX-Trigger")
if trigger == "get_working_hours_row":
return render(
request,
"rentals/partials/work_hour_row.html",
{"hours": working_hours},
)
else:
context = {"form": form, "day_of_week": day_of_week}
return render(
request, "rentals/partials/work_hours_inline_form.html", context
)
def put(self, request, pk):
working_hours = get_object_or_404(RentalWorkingHours, pk=pk)
form = UpdateWorkingHoursForm(request.PUT, instance=working_hours)
if form.is_valid():
form.save()
return render(request, "work_hour_row.html")
day_of_week = working_hours.get_day_of_week_display()
return render(
request,
"rentals/partials/work_hours_inline_form.html",
{"form": form, "day_of_week": day_of_week},
)
Urls.py:
path(
"nuomotojo-platforma/darbo-laikas/atnaujinti/<int:pk>/",
UpdateRentalWorkingHours.as_view(),
name="update_work_hours",
),
Several times I went through all the details, so that I would not miss a letter or something like that. Also went through it with ChatGPT. It can’t find anything specific.
Rimgaudas Jurgaitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.