Monday, June 28, 2021

In need of urgent help for products sorting and displaying

 I've been building a Django E-commerce website and I'm facing problems displaying the sorted products on the products page of my website. I can display the products by category sorting in which I'm using crispy-forms to display the form but I am unable to display the products when sorted by price. I've attached the required files below.  

Do let me know if anything else is needed. 
Thanks for your help in advance

Regards,
Adarsh 

I am having an error this :


 
    
#views.py class Product(ListView):
model = Items
    paginate_by = 6
    template_name = 'products.html'
    ordering = ["-id"]

    def get_queryset(self):
        queryset = super().get_queryset()
        filter = ProductFilter(self.request.GETqueryset)
        return filter.qs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        queryset = self.get_queryset()
        filter = ProductFilter(self.request.GETqueryset)
        context["filter"= filter
        return context

    def search(request):
        sorting = request.GET.get('sorting''-date_added')
        products = Items.objects.all()

        context = {
            'products'products.order_by(sorting),
            'sorting'sorting
        }

        return context


#urls.py
from django.urls import path
from .views import *
from . import views
app_name = 'store'


urlpatterns = [
    path(''HomeView.as_view(), name='home-page'),
    path('checkout'CheckoutView.as_view(), name='checkout-page'),
    path('add-cart/<slug>'views.add_to_cart, name='add-cart'),
    path('cart'CartFunc.as_view(), name='cart-page'),
    path('remove-cart/<slug>'views.remove_from_cart, name='remove-cart'),
    path('products'Product.as_view(), name='products-list'),
    path('product_detail/<slug>'ProductDetails.as_view(), name='product-detail'),
    path('remove-item-from-cart/<slug>/'remove_single_item_from_cart,
         name='remove-single'),
    path('payment'views.payment, name='payment-page'),
    path('about-us'views.about_us, name='about-us'),
    path('contact-us'views.contact_us, name='contact-us'),
    path('blogs'views.blogs, name='blogs'),
    path('terms'views.terms, name='terms'),
]

#products.html
    <!--Filter-->
    <div class="row">
      <div class="container">
        <form method="get" action="{% url 'store:products-list' %} style="width:20%">
          {{filter.form|crispy}}
          <div class="control">
            <div class="select">
              <select name="sorting">
                <option value="-date_added"{% if sorting == '-date_added' %} selected{% endif %}>Newest first</option>
                <option value="price"{% if sorting == 'price' %} selected{% endif %}>Price low-high</option>
                <option value="-price"{% if sorting == '-price' %} selected{% endif %}>Price high-low</option>
              </select>
            </div>
            </div>
          <button type="submit" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-search"></span> Search</button>
        </form> 
      </div>
    </div>
<!--Filter ends here-->

<!-- Displaying products -->
  {% for item in object_list %}
       <div class="col-md-4">
          <div class="product-item">
            <a href="{{ item.get_absolute_url }}"><img src="{{ item.image.url }}" alt=""></a>
           <div class="down-content">
             <a href="{{ item.get_absolute_url }}"><h4>{{ item.title }} ({{ item.size }})</h4></a>
             <h6>
             {% if item.discount_price %}
             <small><del>Rs.{{ item.price }}</del></small>Rs.{{ item.discount_price }}
              {% else %}
              Rs.{{ item.price}}
              {% endif %}
             </h6>
            <p>{{ item.description|truncatechars:40 }}</p>
           </div>
          </div>
         </div>
      {% endfor %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANNRaDArPX61yWAW4zC%2BNcEHzWxdnEzb3T5QqbxBNTqM1xCdnQ%40mail.gmail.com.

No comments:

Post a Comment