Hi Everyone! Long time lurker, the forums have been a great resource for me for the past 8 months as I have debugged my first Django project. I am currently building a website for a non-profit search and rescue team for free. However, I’ve now spent the last week trying to figure out an error that I haven’t been able to overcome.
The high level summary: I have a page that lists upcoming “Events”. Each event can have its details edited via a modal popup (pre-populates current event details). The modal shows/hides via alpine.hs, and htmx swaps the modal’s html to the necessary event to be edited. I keep running into a reverse lookup error when trying trying to implement the hx-post for the edited form.
I have seen similar errors referenced here and on stack overflow but all the situations/root causes didn’t seem like they applied here. I am sure there is probably a basic and simple thing that I am overlooking. I am trying to use carlton’s django-template-partials as well btw.
If anyone sees anything obvious that I am missing, please let me know. I find it very confusing that the template (for the modal & form) can render the event.id but the hx-post=“{% url ‘update_event’ event.id %}” receives an empty string.
Thank You!
The view
@login_required(redirect_field_name=LOGIN_URL, login_url=LOGIN_URL)
def update_event(request: HttpRequest, event_id: int):
if not request.user.has_perm("members.change_event"):
messages.error(request, "You do not have permission to update this event.")
return redirect("all_events")
siteinfo = SiteInfo.objects.get(id=1)
event = Event.objects.get(pk=event_id)
if request.method == "POST":
form = EventForm(request.POST, instance=event)
if form.is_valid():
form.save()
events = Event.objects.filter(date__gte=timezone.now()).order_by("date")
context = {"siteinfo": siteinfo, "events": events}
return render(request, "members/events/all_events.html#event_list", context)
return render(request, "members/events/all_events.html#failure")
partial template (not the event.id after Update Location is rendered before submitting)
{% partialdef edit_event_modal %}
<div class="flex items-center justify-center mb-6">
<div class="flex flex-col items-center justify-center">
<h1 class="text-xl font-bold">Update Location {{ event.id }}</h1>
{% if submitted %}
Your location was updated succesfully!
{% else %}
<form hx-trigger="click:#event-edit-submit"
hx-post="{% url 'update_event' event.id %}"
hx-target="#event-list"
hx-swap="innerHTML">
{% csrf_token %}
{{ form|crispy }}
<div class="justify-center items-center flex">
<button id="event-edit-submit"
type="submit"
@click="showEditEventModal = false"
value="Submit"
class="my-2 mb-3 me-2 ms-2 inline-flex w-1/2 flex-row items-center justify-center rounded-lg bg-misargreen px-2 py-2.5 text-center text-sm font-bold text-white hover:bg-white hover:text-black focus:outline-none focus:ring-4 focus:ring-misargreen dark:bg-misargreen dark:hover:bg-white dark:hover:text-black dark:focus:ring-offset-black">
Submit
</button>
</div>
</form>
{% endif %}
</div>
</div>
{% endpartialdef %}
url
path("events/<int:event_id>/", views.update_event, name="update_event"),
Error message
Request Method: GET <--- why does it say GET and not POST?
Request URL: http://127.0.0.1:8000/members/events/
Django Version: 5.0.6
Python Version: 3.10.10
Template error:
In template my-edited-out-path\members\templates\members\events\all_events.html, error at line 232
Reverse for 'update_event' with arguments '('',)' not found. 1 pattern(s) tried: ['members/events/(?P<event_id>[0-9]+)/\\Z']
222 : <div class="inline bg-red-200 p-2 border border-red-400 text-red-700 px-4 py-2">Event Not Successfully Updated!</div>
223 : {% endpartialdef %}
224 : {% partialdef edit_event_modal %}
225 : <div class="flex items-center justify-center mb-6">
226 : <div class="flex flex-col items-center justify-center">
227 : <h1 class="text-xl font-bold">Update Location {{ event.id }}</h1>
228 : {% if submitted %}
229 : Your location was updated succesfully!
230 : {% else %}
231 : <form hx-trigger="click:#event-edit-submit"
232 : hx-post=" {% url 'update_event' event.id %} "
233 : hx-target="#event-list"
234 : hx-vars="event_id={{ event.id }}"
235 : hx-swap="innerHTML">
236 : {% csrf_token %}
237 : {{ form|crispy }}
238 : <div class="justify-center items-center flex">
239 : <button id="event-edit-submit"
240 : type="submit"
241 : @click="showEditEventModal = false"
242 : value="Submit"
.....Long traceback....
Exception Type: NoReverseMatch at /members/events/
Exception Value: Reverse for 'update_event' with arguments '('',)' not found. 1 pattern(s) tried: ['members/events/(?P<event_id>[0-9]+)/\\Z']