Django:为什么我的自定义过滤器找不到数组中的现有项目?
Django: why does my custom filter not find an existing item in the array?
我是 Django 的初学者。现在我正在做我的第一个项目:为电影活动预订座位。 html 位置图基于 Hall 模型提供的列和行数组。
还有一个Invoice模型,它存储了基于这个事件的预订名额的信息。基于此,我试图禁用已经保留的座位的 html-map 复选框。
这是我的代码:
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
})
事件-map.html
<div class="theatre-map event-map-grid hall-{{ hall }}" id="theatre-map-target">
{% for sit in sits %}
{% if sit|if_in_list:reserved_places %}
<label class="res" id="{{ sit }}" disabled=""></label>
{% else %}
<label class="emp box-label" id="{{ sit }}"></label>
{% endif %}
{% endfor %}
</div>
templatetags/cinema_extras.py
问题如下:尽管 reserved_places 列表包含一些元素,但座位都是免费生成的。这是打印出 reserved_places 的地图图片:
我真的在问自己,这是否可能是某些过滤器错误或其他原因。
感谢阅读的各位!
您的 reserved_places
是一个 字符串列表 ,不是一个整数列表,并且 0
不等于 '0'
。
您应该将它们转换为整数,因此:
reserved_places = []
invoices = Invoice.objects.filter(event=selected_event)
for invoice in invoices:
reserved_places += <strong>map(int</strong>, invoice.invoice_details.split(',')<strong>)</strong>
但是首先使用带有(逗号分隔)整数列表的字符串可能不是存储预留位置的最佳方式(没有双关语意):这使得查询数据库以查找示例变得困难到 invoice
选择了某个项目,并且使用 Invoice
查询以查找预留座位也有点“不方便”。
我是 Django 的初学者。现在我正在做我的第一个项目:为电影活动预订座位。 html 位置图基于 Hall 模型提供的列和行数组。
还有一个Invoice模型,它存储了基于这个事件的预订名额的信息。基于此,我试图禁用已经保留的座位的 html-map 复选框。
这是我的代码:
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
})
事件-map.html
<div class="theatre-map event-map-grid hall-{{ hall }}" id="theatre-map-target">
{% for sit in sits %}
{% if sit|if_in_list:reserved_places %}
<label class="res" id="{{ sit }}" disabled=""></label>
{% else %}
<label class="emp box-label" id="{{ sit }}"></label>
{% endif %}
{% endfor %}
</div>
templatetags/cinema_extras.py
问题如下:尽管 reserved_places 列表包含一些元素,但座位都是免费生成的。这是打印出 reserved_places 的地图图片:
我真的在问自己,这是否可能是某些过滤器错误或其他原因。 感谢阅读的各位!
您的 reserved_places
是一个 字符串列表 ,不是一个整数列表,并且 0
不等于 '0'
。
您应该将它们转换为整数,因此:
reserved_places = []
invoices = Invoice.objects.filter(event=selected_event)
for invoice in invoices:
reserved_places += <strong>map(int</strong>, invoice.invoice_details.split(',')<strong>)</strong>
但是首先使用带有(逗号分隔)整数列表的字符串可能不是存储预留位置的最佳方式(没有双关语意):这使得查询数据库以查找示例变得困难到 invoice
选择了某个项目,并且使用 Invoice
查询以查找预留座位也有点“不方便”。