在表单提交中,我在request.POST中获取了csrf_token和quantity,但没有获取ticket复选框。 我有很多复选框,但在示例中省略了它们。
我是否需要使用JS?
将值添加到submit标记下的输入元素<form id="tour-form" role="form" class="tour-form clearfix" method="post"
action="{% url 'tour:add-to-cart' tour_id=tour.pk slug=tour.get_slug %}" novalidate>
{% csrf_token %}
<strong>You are booking</strong>
<span>{{tour.name}}</span>
<ul>
<li>
<label class="custom-check" for="tour-form-tickets">
<input type="checkbox" id="tour-form-tickets" name="tickets" value="1" price="{{tranport_prices_total|floatformat:'0'}}"/>
<i></i>
<strong>Corresponding airplane packages</strong> -
<span>{{tranport_prices_total|floatformat:"0"}} €</span>
</label>
</li>
</ul>
<span class="total-box">
Total <span>0 €</span>
</span>
<input type="submit" value="order tour"/>
<input type="hidden" name="quantity" value="1" />
</form>
观点是:
def tour_add_to_cart(request, slug, tour_id, hotel=None, shuttle=None):
if not request.method == 'POST':
return redirect(reverse(
'tour:details',
kwargs={'tour_id': tour_id, 'slug': slug}))
tours = tour_for_cart(user=request.user)
tour = get_object_or_404(tours, pk=tour_id)
form, cart = handle_cart_form(request, tour, create_cart=True)
if form.is_valid():
form.save()
if request.is_ajax():
response = JsonResponse({'next': reverse('cart:index')}, status=200)
else:
response = redirect('cart:index')
else:
if request.is_ajax():
response = JsonResponse({'error': form.errors}, status=400)
else:
response = tour_details(request, slug, tour_id, form)
if not request.user.is_authenticated:
set_cart_cookie(cart, response)
return response