0

Hi I'm trying to develop an online store website using Django and I don't know why, but my price counter is not working. It was working all fine before I added some pagination, and now it's not adding all the values.Can anyone please help me out? My views.py:

def cart(request):
    cart = Cart.objects.all()[0]
    context = {"cart":cart}
    template = 'shopping_cart/cart.html'
    return render(request, template, context)


def add_to_cart(request, slug):
    cart = Cart.objects.all()[0]
    try:
        product = Product.objects.get(slug=slug)
    except Product.DoesNotExist:
        pass
    except:
        pass 
    if not product in cart.products.all():
        cart.products.add(product)
        messages.success(request, mark_safe("Product added to cart. Go to <a href='cart/'>cart</a>"))
        return redirect('myshop-home')
    else:
        cart.products.remove(product)
        messages.success(request, mark_safe("Product removed from cart"))

    new_total = 0.00
    for item in cart.products.all():
        new_total += float(item.price)

    cart.total = new_total
    cart.save()


    return HttpResponseRedirect(reverse('cart'))

My index.html(where I added pagination):

{% extends 'base.html' %} 

{% block content %}
    <h1>Products</h1>
    <div class="container-md">
        <div class="row">
            {% for product in products %}
                <div class="col">
                    <div class="card-deck" style="width: 18rem;">
                      <img src="{{ product.image_url }}" class="card-img-top" alt="...">
                      <div class="card-body">
                        <a class="card-title text-dark" href="{% url 'detail-view' product.slug %}">{{ product.name }}</a>
                        <p class="card-text">${{ product.price }}</p>
                        {% if not product.cart_set.exists %}
                              <a href="{% url 'add-to-cart' product.slug %}" class="btn btn-dark">Add to Cart</a>
                        {% else %}
                             <a href="{% url 'add-to-cart' product.slug %}" class="btn btn-dark">Remove from Cart</a> 
                        {% endif %}

                      </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
    <div>
        <ul class="pagination justify-content-center">
            {% if is_paginated %}

              {% if page_obj.has_previous %}
                <a class="btn btn-outline-dark mb-4" href="?page=1">First</a>
                <a class="btn btn-outline-dark mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
              {% endif %}

              {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                  <a class="btn btn-dark mb-4 " href="?page={{ num }}">{{ num }}</a>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                  <a class="btn btn-outline-dark mb-4" href="?page={{ num }}">{{ num }}</a>
                {% endif %}
              {% endfor %}

              {% if page_obj.has_next %}
                <a class="btn btn-outline-dark mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
                <a class="btn btn-outline-dark mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
              {% endif %}

            {% endif %}
        </ul>
    </div>
{% endblock %}

My homeview:

class homeview(ListView):
    model = Product
    paginate_by = 6
    context_object_name = 'products'
    template_name = 'index.html'

My urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('cart/', sc_views.cart, name='cart'),
    path('cart/<str:slug>/', sc_views.add_to_cart, name='add-to-cart'),
    path('', include('products.urls'))
]
  • 1. That is a poorly written exception 2. Bad querying that can be greatly simplified 3. Realllyyyy poor method of summation. Cough aggregates. 4. This shows you should in no way be trying to roll your own payments / carts before you have a big problem when this hits production - if it does 5. This leads to you (hopefully) using a package where others have already solved it. – Pythonista Mar 28 '20 at 18:30
  • I'm sorry but I asked for help...... –  Mar 29 '20 at 06:43

0 Answers0