0

嗨,我正在尝试使用 Django 开发一个在线商店网站,但我不知道为什么,但我的价格计数器无法正常工作。在我添加一些分页之前它工作得很好,现在它没有添加所有的值。有人可以帮我吗?我的意见.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'))

我的 index.html(我在其中添加了分页):

{% 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 %}

我的主页:

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

我的 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'))
]
4

0 回答 0