From my base.html:
<div id="button_container">
<button class="button"><a href="{% url 'home' %}"> HOME </a></button>
<button class="button"><a href="{% url 'rooms' %}"> ROOMS & SUITES </button>
<button class="button"><a href="{% url 'roomBookingsList' %}"> ROOM BOOKINGS </button>
</div>
Urls.py:
path('rooms/mybooking', views.roomBookingsList, name='roomBookingsList'),
Views.py:
def roomBookingsList(request):
suiteBookingList = Suite_booking.objects.all()
context = {
'suiteBookingList': suiteBookingList,
}
return render (request, 'roomBookingsList.html', context=context)
roomBookingsList.html:
{% for suiteBookingList in suiteBookingList %}
<li> {{ suiteBookingList }}</li>
{% endfor %}
So when I run it and click the ROOM BOOKINGS from the base.html, it will show me whatever rooms I have booked. No problem here. I also want to add an edit button here so I can update the booking if I accidentally typo the name or something.
So I added in the roomBookingsList.html:
{% for suiteBookingList in suiteBookingList %}
<li> {{ suiteBookingList }}</li> - <a href="{% url 'edit_suite' suite.id %}">Edit</a>
{% endfor %}
Added in urls.py:
path('rooms/suite/edit/<int:suite_id>', views.edit_suite, name='edit_suite'),
Added in views.py:
def edit_suite(request, suite_id):
if request.method == 'POST':
suite = Suite_booking.objects.get(pk=suite_id)
form = BookingForm(request.POST, instance=suite)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('roomBookingsList'))
else:
suite = Suite_booking.objects.get(pk=suite_id)
fields = model_to_dict(suite)
form = BookingForm(initial=fields, instance=suite)
context = {
'form': form,
'type': 'edit',
}
return render(request, 'roomBookingsList.html', context=context)
Then I tried to run it again, and this time when I click the ROOM BOOKINGS button, it gave an error:
NoReverseMatch at /system/rooms/mybooking
Reverse for 'edit_suite' with arguments '('',)' not found. 1 pattern(s) tried: ['system/rooms/suite/edit/(?P<suite_id>[0-9]+)$']
In the traceback there's this:
D:\ssip\hotel\system\views.py, line 59, in roomBookingsList
59. return render (request, 'roomBookingsList.html', context=context)
I tried searching all the files I used for the ('',)
but didn't find any. I also looked over my def roomBookingsList
but didn't find anything odd in it. I already tried searching the error but most answers were OP forgetting the dot or underscore when passing args.
Is it because I'm calling Suite_booking
object in both the def roomBookingsList
and edit_suite
? What should I change here?
Sorry it's long and thank you in advance.
It seems like suite.id
does not exist. Shouldn't it be suiteBookingList.id
instead?
{% for suiteBookingList in suiteBookingList %}
<li> {{ suiteBookingList }}</li> - <a href="{% url 'edit_suite' suiteBookingList.id %}">Edit</a>
{% endfor %}
And though it works, I would recommend not using the same name as your variable to iterate:
{% for suite in suiteBookingList %}
<li> {{ suite }}</li> - <a href="{% url 'edit_suite' suite.id %}">Edit</a>
{% endfor %}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments