Using breadcrumbs with django-filter, QueryDict trouble - django

I'm using django-filter to drill down and would like to create breadcrumbs for each item that was filtered. For example:
Price ranges:
10,000+
5,000-9,999
1,000-4,999
0-999
Bedrooms:
4
3
2
1
Each of the items under Price ranges and Bedrooms would be a link to drill down in a queryset.
I'd like to create a breadcrumb such as Price range 0-999 or Bedrooms 3 if the user were to click those links, and then show Price range 0-999 > Bedrooms 3 or Bedrooms 3 > Price range 0-999 when they click a second link.
The breadcrumbs should maintain order (the part I'm having trouble with) and work for any number of attributes. Clicking a link in the breadcrumb trail should apply the filter clicked on and all filters before it in the trail.
I'd like to create an empty QueryDict and then iterate through request.GET to build the QueryDict up as I output the breadcrumbs, but for some reason QueryDict iterates through its elements backwards (see the documentation).
What's the cleanest way to accomplish this? Does anyone know why QueryDict works this way? (I imagine there's a use-case I'm missing.) Any advice is appreciated.

keep track of the order in sessions. so when the first filter is clicked (eg 3 beds) store it. then if another one is clicked, build your bread crumbs from sessions. say the second one was 0-999 you'd pull any existing breadcrumbs (in this example you'd find the 3 beds) and then tack on the latest (0-999).

Related

I dont know how to pass two queryset parameters into one URL

I really struggled to explain my problem and the only way I found it would be possible is - through screenshots as I have a lot of code and I am not sure what is really needed here. So if you want any code, tell me I will add.
The numbers on the pictures indicate the order.
Choosing the category
Selecting the category it redirects me to - /products_list?category=(that category_id)
Filtering through brand in that category
Now please pay attention to the URL and what happens after I have chosen the brand I want to filter with.
Back on the first page
Problem is here:
Now I am back on the first page, where are all the products but I wanted it to stay on that URL where are that kind of category products.
What I wanted to happen? Instead of it taking me to the page where are ALL the products and then doing the filtering, I want it to stay on that category page and return the filtered products there.
The brand dropdown menu also should only show that category products that I am in, not all.
You need to pass the other parameters as well. So that means that for two parameters category_id and brand, you create a URL that looks like:
{% url 'product-list' %}?category={{ category_id|urlencode }}&brand={{ brand|urlencode }}
If you thus already filtered the category down, you pass the category_id to the template, and render the URLs with the ?category={{ category_id|urlencode }} part.

Retrieving random records from database depending on an attribute value until a limit, and complete with other if the limit is not reached

I'm using Django with Postgres.
On a page I can show a list of featured items, let's say 10.
If in the database I have more featured items than 10, I want to get them random/(better rotate).
If the number of featured item is lower than 10, get all featured item and add to the list until 10 non-featured items.
Because the random takes more time on database, I do the sampling in python:
count = Item.objects.filter(is_featured=True).count()
if count >= 10:
item = random.sample(list(Item.objects.filter(is_featured=True))[:10])
else:
item = list(Item.objects.all()[:10])
The code above miss the case where there less than 10 featured(for example 8, to add 2 non-featured).
I can try to add a new query, but I don't know if this is an efficient retrive, using 4-5 queries for this.
The best solution I could find is this:
from itertools import chain
items = list(chain(Item.objects.filter(is_featured=True).order_by('?'), Item.objects.filter(is_featured=False).order_by('?')))[:10]
In this way, the order of the querysets are retained, but downside is that items becomes a list not a Queryset. You can see more details in this SO Answer. FYI: there are some fantastic solutions like using Q or pipe but they don't retain order of queryset.
SQL method: You can achieve that with an SQL statement like this:
SELECT uuid_generate_v4(), *
FROM table_name
ORDER BY NOT is_featured, uuid_generate_v4()
LIMIT 10;
Explain: The generated UUID should simulate randomness (for the purpose of e-commerce, this should suffice). While sorting the rows by NOT is_featured will put the is_featured rows on top; and automatically flow the rows down to 10 limits if it run out of featured items.

Return to scroll position when using django endless pagination

1) Initial load of list page shows 50 items.
2) By "clicking show more" using django endless pagination, there are 100 items on the page now.
3) follow link to 90th item in list and go to another page
4) click back button on the browser
The problem is that after step 4, the list contains only the first 50 items.The last 50 items is not displayed.
Is it possible to return to position of 90th item on list page by hitting the back button
I think it's not special to Django. We can use url hash to return to specified place. So,
1. in html, code snippet <div id="test">...</div>
2. url like http://xxxx/#test will be located to div with id 'test'
But, this way cannot remember the exact position.
For example, cannot remember position between two div each with an id.

Get products according to chosen value

I am creating a shop with Opencart 1.5.6 & I'm new to opencart & php so please help me
I searched a lot but all of results are talking about adding a filter in SORT drop down list like manufacturer ..
but I don't want to sort,
I added a new custom field called COLOR to product and i want to create a new drop down list contains RED,BLUE,& BLACK options and it will get all the products with this chosen color.
I tried to create a drop down list like "Sort By:" one,
but i can't because i have no experience with PHP or Opencart.
PLEASE HELP ME !! and thanks in advance :)
As you said you are beginner so just go through step by step
In Opencart there is a feature called filter which exactly fulfills your requirement
**Step1 (Creating Filter)**
Admin panel>catalog>filter>Inset a new filter
->Filter Group Name "Color".
->click on add filter and add your colors "Red, Green, Blue...."
**Step2 (Adding Filter attribute to Product)**
Admin panel>catalog>products>edit product
->under the link tab there is a filter add the filter to product you want
eg if the product is red in color add "red"
->Do this to all product you wanted to be filtered.
**Step3 (Adding Filter Scope to category)**
Admin panel>catalog>categories>edit category
->under the data tab there is a filter add the filter you want to be displayed like "red, green etc.."
->Do this to all category you wanted filter to be displayed.
**Step4 (Enable Filter module or setting layout)**
Admin panel>Extensions>Modules
->Find Filter and click on install
->Now Edit the filter
->click on add module
->Set Layout to Category, Position to Content Top and Status to Enable
->Click on Save
Now you are done, In category you can see the filter feature in category,
If you still find difficulties you can follow the official documentation http://docs.opencart.com/display/opencart/Filters
Hope this helps.

Google Charts - Multiple Category Filters - Restricting options?

I have 3 suppliers, 40 brands and 120 products, all displayed in three category filters. When a user selects one supplier from the Supplier filter, I want the Brand filter to hide all brands that are not provided by that supplier. Likewise, when a brand is selected, I want to hide the irrelevant products from the Products filter. I don't really care about what's being shown in the table at this point.
Is there any way to implement something like this?
Heres an example for you -
http://jsfiddle.net/danabnormal/cjn2tbbm
You can do this by creating a Dashboard. At around line 61 you can see that the selection made in the 'Sex' drop down is bound to the 'Name' dropdown, thus limiting what Names can be selected.
dashboard.bind(filterSex, filterName);
dashboard.bind(filterSex, pieChart);