I am a beginner in Django. Right now i am working on my first project: reservation of sits for cinema event. The html map of sits is based on the array of cols and rows, provided from the Hall model.
There is also an Invoice model, which stores the information about reserved places, based on this event. Based on this, i am trying to disable the html-map checkboxes of the sit places, which are already reserved.
Here is my code:
views.py
def event_map(request, slug, event_id):
selected_event = Event.objects.get(pk=event_id)
movie = selected_event.film
hall = selected_event.hall
time = selected_event.get_time_slot_display()
date_event = selected_event.get_week_day_display()
sits = list(range(0, hall.hall_sits))
cols = list(range(0, hall.hall_cols))
hall_information = generate_hall_information(hall.hall_name)
reserved_places = list()
invoices = Invoice.objects.all().filter(event=selected_event)
for invoice in invoices:
reserved_places = invoice.invoice_details.split(',')
reservation_form = Reservation()
if request.method == 'POST':
print(request.POST)
form = Reservation(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.invoice = request.user
post.event = selected_event
post.save()
return render(request, 'pages/event_map.html', {
'movie_name': movie.name,
'hall': hall,
'time': time,
'date': date_event,
'sits': sits,
'cols': cols,
'hall_information': hall_information,
'reservation_form': reservation_form,
'reserved_places': reserved_places,
'test': reserved_places
})
event-map.html
<div id="theatre-map-target">
{% for sit in sits %}
{% if sit|if_in_list:reserved_places %}
<label id="{{ sit }}" disabled=""></label>
{% else %}
<label id="{{ sit }}"></label>
{% endif %}
{% endfor %}
</div>
templatetags/cinema_extras.py
The problem is following: the sit places are all generated as free, although reserved_places list consists of some elements. Here is a picture of the map with the reserved_places printed out:
I am really asking myself, if this could be some filter error or something else. Thanks to everyone, who have read it!
CodePudding user response:
Your reserved_places
is a list of strings, not a list of integers, and 0
is not equal to '0'
.
You should convert these to ints, so:
reserved_places = []
invoices = Invoice.objects.filter(event=selected_event)
for invoice in invoices:
reserved_places = map(int, invoice.invoice_details.split(','))
But using a string with a (comma separated) list of integers might not be the best way to store reserved places in the first place (no pun intended): it makes it hard to query the database to look for example to an invoice
where a certain item is selected, and also makes it a bit "incovenient" to query with Invoice
s to find the reserved seats.