In need of urgent help for products sorting and displaying

47 views
Skip to first unread message

Adarsh Sarda

unread,
Jun 28, 2021, 6:52:58 AM6/28/21
to django...@googlegroups.com
 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 :
image.png

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

Kasper Laudrup

unread,
Jun 28, 2021, 7:11:31 AM6/28/21
to django...@googlegroups.com
On 28/06/2021 12.51, Adarsh Sarda wrote:
>  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
>

Check the "action" parameter to your form. Where exactly does the string
given to that parameter end?

That should help you figure out how to fix it.

Kind regards,

Kasper Laudrup

OpenPGP_signature

Aritra Ray

unread,
Jun 28, 2021, 7:59:23 AM6/28/21
to django...@googlegroups.com
We've realised the error but are unable to fix it. We'll be grateful if you can solve it for us as we've been stuck on this for a long time. The search function in the views.py is conditioning the products via products.html filter form which is requesting for a "products-list url" which looks like this: path('products', Product.as_view(), name='products-list').
The problem is the url pattern. Can you help us resolve this issue?
The GitHub link to our repo is https://github.com/First-project-01/Django-ecommerce


Will be of great help
Regards,
Aritra.


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bec6b39c-53a2-2905-eb4c-77cb24ba6c25%40stacktrace.dk.

Kasper Laudrup

unread,
Jun 28, 2021, 8:07:32 AM6/28/21
to django...@googlegroups.com
On 28/06/2021 13.58, Aritra Ray wrote:
> We've realised the error but are unable to fix it. We'll be grateful if
> you can solve it for us as we've been stuck on this for a long time.

I cannot solve your problems for you, but I and many others will be
happy to help if you provide some details on exactly what problems
you're having.

The idea of a mailing list/forum like this is helping each other and
learning from each other.

Kind regards,

Kasper Laudrup

OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages