I've been looking for this question and couldn't find any, sorry if it's duplicated.
I'm building some kind of ecommerce site, similar to ebay. The problem i have arise when i'm trying to browse through "categories" and "filters". For example. You can browse the "Monitor" category. That will show you lots of monitors, and some filters (exactly the same as ebay) to apply them. So, you go to "monitors", then you have filters like:
Type: LCD - LED - CRT
Brand: ViewSonic - LG - Samsung
Max Resolution: 800x600 - 1024x768
And those filters will be appended to the URL, following with the example, when you browse monitors the URL could be something like:
store.com/monitors
If you apply the "Type" filter:
store.com/monitors/LCD
"Brand":
store.com/monitors/LCD/LG
"Max Resolution":
store.com/monitors/LCD/LG/1024x768
So, summarizing, the URL structure would be something like:
/category/filter1/filter2/filter3
I can't figure out how to do it really. The problem is that filters can be variable. I think in the view will need to use **kwargs but i'm not really sure.
Do you have any idea how to capture that kind of parameters?
Thanks a lot!
Ben, I hope this will help you
urls.py
from catalog.views import catalog_products_view
urlpatterns = patterns(
'',
url(r'^(?P<category>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/(?P<filter3>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
)
view.py
def catalog_products_view(request, category, filter1=None, filter2=None, filter3=None):
# some code here
or
def catalog_products_view(request, category, **kwargs):
filter1 = kwargs['filter1']
filter2 = kwargs['filter2']
....
filterN = kwargs['filterN']
# some code here
You could add this to your urls:
url(r'^(?P<category>\w)/(?P<filters>.*)/$', 'myview'),
And then myview would get the parameters of category and filters. You could split filters on "/" and search for each part within the Filters table.
Does that make sense?
how do you intend to decide what aspect is being filtered by? Do you have a list of accepted keywords for each category? ie how does the server know that
/LCD/LG/
means type=LCD, brand=LG
but
/LG/LCD
doesn't mean type=LG, brand=LCD etc
Is there any reason you don't want to use GET params, e.g.
.../search/?make=LD&size=42
Related
I want to pull all my data from typeforms. I want to use a list with all "form IDs" and I'm using the typeform client wrapper to do that.
When I'm using the .list() method (which should give me all forms) it only gives me only the ones on the first page.
I am looking for a way to specify how many pages/ what page I want to get the contents of.
Any help is greatly appreciated!
from typeform import Typeform
typeform = Typeform(api_key)
form = typeform.forms
forms = form.list()
print(forms)
#gives me:
total_items': 60,
'page_count': 6,
'items': #here only the first 10/60
Assuming you are using this package
Looks you can paginate using form.list() passing some extra parameters.
According to the Typeform documentation, the maximum number of forms you can retrieve per page is 200.
Which should be plenty for your needs.
Passing page size to forms.list function will get you all your forms.
forms: dict = typeform.forms.list(1,200)
Let me know if you encounter any issues.
I am trying to search for a list of values in multiple columns in postgres (via django). I was able to use SearchQuery and SearchVector and this works great if one of the search values matches a full word. I was hoping to use icontains so that partial strings could also be used in the search. Is this possible and if so could someone point me in the right direction. Here is an example of my approach below.
Example Data:
Superhero.objects.create(
superhero='Batman',
publisher='DC Comics',
alter_ego='Bruce Wayne',
)
Superhero.objects.create(
superhero='Hulk',
publisher='Marvel Comics',
alter_ego='Bruce Banner',
)
Django filter:
from django.contrib.postgres.search import SearchQuery, SearchVector
query = SearchQuery('man') | SearchQuery('Bruce')
vector = SearchVector('superhero', 'alter_ego', 'publisher')
queryset = queryset.annotate(search=vector).filter(search=query)
This would return the Hulk record but I am hoping I can somehow use like 'icontains' so that when searching for 'man' the Batman record would also be returned. Any help is appreciated!
You can apply icontains to the filter like:
queryset = queryset.annotate(search=vector).filter(search__icontains=query)
So SearchQuery and SearchVector are a part of Django's Full Text searching functionality and it doesnt look like you can achieve what I was wanting to do with these functions. I have taken a different approach thanks to Julian Phalip's approach here.. https://www.julienphalip.com/blog/adding-search-to-a-django-site-in-a-snap/
I have a problem sorting Authors by Author.likes or Author.views .
Lets me first show you my codes snippets :
views.py
from sqlalchemy import func
#app.route('/authors/')
def authors():
page = request.args.get('page', 1, type=int)
pagination = Author.query.order_by(func.count(Author.likes)).paginate(page, per_page=app.config['AUTHORS_PER_PAGE'], error_out=False)
Here am using the func.count() function to sort the authors ordered by likes.
The problem is am getting just the most liked author , in fact i have 12 of them in the database each one has his likes, so i need to sort them all by likes from the highest one to the lowest .
I tried a different ways but without avail , please any help !
Remove func.count and just order by Author.likes, it's already a count. func.count is for counting groups of things. Currently, you're grouping everything into one group and so you only get one item back.
In django doc proposed instead GET method use urlpatterns, and made ​​convenient way to handle these variables. But if at least one of the variables is not necessary I'll have to write more lines in url.py. I like that I can avoid this?
Example:
If I want to take a sample of posts in a given year, in urlpatterns I should add something like this:
url(r'^articles/(?P<year>\d{4})/$', 'news.views.show_archive'),
url: .../articles/1994/
If I want to make the sample positions for a particular month a specific year, in urlpatterns I should add something like this:
url(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.show_archive'),
url: .../articles/2003/03/
But if I want to see the records of all the years created particular month of year I have to add also this line:
url(r'^articles/(?P<month>\d{2})/$', 'news.views.show_archive'),
url: .../articles/03/
But I would like to do only one line that specifies the maximum set of variables, but that would process any of these URL.
To be honest I'm not sure that this is possible.
regexps can have optional parts, and view functions can have optional arguments. Also, you can still use querystrings (through request.GET) for what has no business being part of the URL (like query terms for a "search" view, ordering and filtering for a listing view, etc).
The point of using urlpatterns instead of querystrings is to build clean "semantic" urls, ie /blog/posts/<post_id>/ instead of /blog/posts/?post_id=<post_id>.
you could try like this
url(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.show_archive'),
def show_archive(request,year=None,month=None):
if year and month:
.....................
elif year:
.....................
elif month:
....................
I'm building a small PyGTK application and I have an text input field (currently a ComboBoxEntry) which is populated with a few values that the user should be able to choose from.
I think what I want to do is to filter out the matching fields and only show those ones so the user using the keyboard arrows can choose one of the matching ones.
To give some background the predefined values are a bunch of urls and the user should be able to choose from theese or fill in a new one.
Example:
the predefined urls:
http://www.google.com
http://www.google.com/android
http://www.greatstuff.com
http://www.facebook.com
When a user types 'http://www.g'
The three URLs starting with that string is to be shown (in some way) and when typeing 'http://www.goog' the two starting with that is to be shown
Any Ideas?
An Entry with an EntryCompletion seems more appropriate than a ComboBoxEntry. As always, the tutorial is a good start.
It's very easy to set up when the predefined URLs list is small and fixed.
You just need to populate a ListStore:
# simplified example from the tutorial
import gtk
urls = [
'http://www.google.com',
'http://www.google.com/android',
'http://www.greatstuff.com',
'http://www.facebook.com',
]
liststore = gtk.ListStore(str)
for s in urls:
liststore.append([s])
completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)
entry = gtk.Entry()
entry.set_completion(completion)
# boilerplate
window = gtk.Window()
window.add(entry)
window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()
gtk.main()
Users are not likely to bother typing "http://" or even "www.", so you probably want to match any part of the URL (e.g. just "og" works!):
def match_anywhere(completion, entrystr, iter, data):
modelstr = completion.get_model()[iter][0]
return entrystr in modelstr
completion.set_match_func(match_anywhere, None)
This will test every value in the ListStore for a match, so it's not scalable to huge lists (I mean huge; a 1000 works fine).
Be sure to play with the various options of EntryCompletion, to configure the most pleasant behavior.
You may want to look at how Deskbar Applet's Cuemiac does it.
Well, you obviously want to deal with prefixes so you'll probably want to use some sort of trie. Of course, there are issues to deal with. For instance, after a person has typed in a few letters ( or maybe even just one) you will want to either traverse the rest of the branches of the trie to find suggestions, or have suggestions stored in each node. A lot of these sorts of decisions depend on how many possible suggestions you plan on having.