Я пытаюсь создать корзину для магазина.При попытке расширить файл shop base.html
я получаю сообщение об ошибке «Объект Generator не может быть интерпретирован как целое число» в строке 37, где начинается цикл for.Также форма не отображается. Эта форма используется для указания количества проектов, которые клиент хочет приобрести
вот моя корзина detail.html
шаблон
{% extends 'gallery/Base.html' %}
{% load static %}
{% block title %}
Your Shopping Cart
{% endblock %}
{% block content %}
<div class="container">
<div class="row" style="margin-top: 6%">
<h2>Your Shopping Cart
<span class="badge pull-right">
{% with totail_items=cart|length %}
{% if cart|length > 0 %}
My Shopping Order:
<a href="{% url "cart:cart_detail" %}" style="color: #ffffff">
{{ totail_items }} item {{ totail_items|pluralize }}, Kshs. {{ cart.get_total_price }}
</a>
{% else %}
Your cart is empty.
{% endif %}
{% endwith %}
</span>
</h2>
<table class="table table-striped table-hover">
<thead style="background-color: #5AC8FA">
<tr>
<th>Image</th>
<th>Product</th>
<th>Quantity</th>
<th>Remove</th>
<th>Unit Price</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{% for item in cart %}
{% with product=item.product %}
<tr>
<td>
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %} {{ product.image.url }} {% else %} {% static 'img/default.jpg' %} {% endif %}" alt="..." style="height: 130px; width: auto">
</a>
</td>
<td>{{ product.name }}</td>
<td>
<form action="{% url "cart:cart_add" product.id %}" method="post">
{% csrf_token %}
{{ item.update_quantity_form.quantity }}
{{ item.update_quantity_form.update }}
<input type="submit" value="Update" class="btn btn-info">
</form>
</td>
<td>
<a href="{% url "cart:cart_remove" product.id %}">Remove</a>
</td>
<td>kshs. {{ item.price }}</td>
<td>kshs. {{ item.total_price }}</td>
</tr>
{% endwith %}
{% endfor %}
вот мои взгляды.py
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST # to post the form
from gallery.models import Product
from .cart import Cart
from .forms import CartAddProductForm
# Create your views here.
@require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update'])
return redirect('cart:cart_detail')
вот мой urls.py
from django.conf.urls import url
from . import views
app_name = 'cart'
urlpatterns = [
url(r'^$', views.cart_detail, name='cart_detail'),
url(r'^add/(?P<product_id>\d+)/$', views.cart_add, name='cart_add'),
]
вот шаблон галереи base.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Project</title>
<meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=yes">
<meta name="description" content=" Project">
<meta name="Context" content="Course">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style>
body {
font: 20px "Montserrat", sans-serif;
line-height: 1.8;
color: #f5f6f7;
}
body {
padding-top: 54px;
margin-bottom: 100px;
}
</style>
</head>
<body>
{% include 'gallery/navbar.html' %}
<!------------------------------block content------------------------------->
<div class="container">
<div class="row" style="margin-top: 6%">
<button class="btn btn-info">
{% with totail_items=cart|length %}
{% if cart|length > 0 %}
My Shopping Order:
<a href="{% url "cart:cart_detail" %}" style="color: #ffffff">
{{ totail_items }} item {{ totail_items|pluralize }}, Kshs. {{ cart.get_total_price }}
</a>
{% else %}
Your cart is empty.
{% endif %}
{% endwith %}
</button>
</div>
</div>
{% block content %}
{% endblock %}
<!-------------------------------- Footer ----------------------------------->
{% include 'gallery/footer.html' %}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>