Подскажите пожалуйста как сделать, чтоб при покупке товара очищалась корзина. И еще есть проблема в том, что без переавторизации не могу сделать больше 1 заказа (кидает на завершение покупки прошлого товара)
Суть работы: после нажатия кнопки checkout товар отправляется в заказы и следующее поле заполнение адреса, после адреса вылезает конечная форма, где указано сколько и чего купили (cюда вот и кидает при следующем заказе)
корзина с товарами:
{% extends "base.html" %}
{% block content %}
<h1>Cart</h1>
{% if cart.products.exists %}
<table class="table cart-table">
<thead>
<tr>
<th>#</th>
<th>Название: </th>
<th>Цена: </th>
</tr>
</thead>
<tbody class='cart-body'>
{% for product in cart.products.all %}
<tr class='cart-product'>
<th scope="row">{{ forloop.counter }}</th>
<td><a href='{{ product.get_absolute_url }}'>{{ product.title }}</a>
{% include 'carts/snippets/remove-product.html' with product_id=product.id %}
</td>
<td>{{ product.price }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="2"></td>
<td><b>Subtotal</b> $<span class='cart-subtotal'>{{ cart.subtotal }}</span></td>
</tr>
<tr>
<td colspan="2"></td>
<td><b>Total</b> $<span class='cart-total'>{{ cart.total }}</span></td>
</tr>
<tr>
<td colspan="2"></td>
<td><a class='btn btn-lg btn-success' href='{% url "cart:checkout" %}'>Checkout</a></td>
</tr>
</tbody>
</table>
<div class='cart-item-remove-form' style='display:none'>
{% include 'carts/snippets/remove-product.html' %}
</div>
{% else %}
<p class='lead'>Корзина пуста</p>
{% endif %}
{% endblock %}
urls.py:
url(r'^$', cart_home, name='home'),
url(r'^checkout/success/$', checkout_done_view, name='success'),
url(r'^checkout/$', checkout_home, name='checkout'),
url(r'^update/$', cart_update, name='update'),
views.py:
def checkout_home(request):
cart_obj, cart_created = Cart.objects.new_or_get(request)
order_obj = None
if cart_created or cart_obj.products.count() == 0:
return redirect("cart:home")
login_form = LoginForm(request=request)
guest_form = GuestForm(request=request)
address_form = AddressCheckoutForm()
# billing_address_id = request.session.get("billing_address_id", None)
shipping_address_id = request.session.get("shipping_address_id", None)
billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request)
address_qs = None
# has_card = False
if billing_profile is not None:
if request.user.is_authenticated():
address_qs = Address.objects.filter(billing_profile=billing_profile)
order_obj, order_obj_created = Order.objects.new_or_get(billing_profile, cart_obj)
if shipping_address_id:
order_obj.shipping_address = Address.objects.get(id=shipping_address_id)
del request.session["shipping_address_id"]
if shipping_address_id:
order_obj.save()
# has_card = billing_profile.has_card
if request.method == "POST":
"проверьте, что заказ выполнен"
is_prepared = order_obj.check_done()
if is_prepared:
did_charge, crg_msg = billing_profile.charge(order_obj)
if did_charge:
order_obj.mark_paid()
request.session['cart_items'] = 0
del request.session['cart_id']
if not billing_profile.user:
'''
is this the best spot?
'''
billing_profile.set_cards_inactive()
return redirect("cart:success")
else:
print(crg_msg)
return redirect("cart:checkout")
context = {
"object": order_obj,
"billing_profile": billing_profile,
"login_form": login_form,
"guest_form": guest_form,
"address_form": address_form,
"address_qs": address_qs,
# "has_card": has_card,
"publish_key": STRIPE_PUB_KEY,
}
return render(request, "carts/checkout.html", context)
форма адреса и вывод информации о заказе:
{% if not billing_profile %}
<div class='row text-center'>
<div class='col-12 col-md-6'>
<p class='lead'>Вход</p>
{% include 'accounts/snippets/form.html' with form=login_form next_url=request.build_absolute_uri %}
</div>
<div class='col-12 col-md-6'>
Продолжить как Гость
{% url "guest_register" as guest_register_url %}
{% include 'accounts/snippets/form.html' with form=guest_form next_url=request.build_absolute_uri action_url=guest_register_url %}
</div>
</div>
{% else %}
{% if not object.shipping_address %}
<div class='row'>
<div class='col-md-10'>
<p class='lead'>Адрес доставки</p>
<hr/>
</div>
<div class='col-md-5'>
{% url "checkout_address_create" as checkout_address_create %}
{% include 'addresses/form.html' with form=address_form next_url=request.build_absolute_uri action_url=checkout_address_create address_type='shipping' %}
</div>
<div class='col-md-5'>
{% url 'checkout_address_reuse' as checkout_address_reuse %}
{% include 'addresses/prev_addresses.html' with address_qs=address_qs next_url=request.build_absolute_uri address_type='shipping' action_url=checkout_address_reuse %}
</div>
</div>
{% else %}
<h1>Завершить Оформление Заказа</h1>
<p>Детали карты: {% for product in object.cart.products.all %}{{ product }}{% if not forloop.last %}, {% endif %}{% endfor %}</p>
<p>Адрес доставки: {{ object.shipping_address.get_address }}</p>
<p>Платежный адрес: {{ object.shipping_address.get_address }}</p>
<p>Способ оплаты: {{ billing_profile.default_card }} (<a href="{{ billing_profile.get_payment_method_url }}?next={{ request.build_absolute_uri }}">Change</a>)</p>
<p>Корзина Всего: {{ object.cart.total }}</p>
<p>Итог Доставки: {{ object.shipping_total }}</p>
<p>Итог Заказа: {{ object.total }}</p>
<form class='form' method='POST' action="">{% csrf_token %}
<button type='submit' class='btn btn-success'>Заказать</button>
</form>
{% endif %}
{% endif %}