Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Search feature

376 views
Skip to first unread message

Mary

unread,
Feb 22, 2007, 5:07:29 PM2/22/07
to Django users
How can i add a search feature to my website?

Can anybody help me in this

Thank you in advance;
Mary Adel

Aidas Bendoraitis

unread,
Feb 23, 2007, 6:37:27 AM2/23/07
to django...@googlegroups.com
Just filter searchable models according the searchable fields by the
user-input. You'll need a form with a field for search string. And a
view for results with something like:
flatpages = FlatPage.objects.filter(Q(title__contains=search_str) |
Q(content__contains=search_str))
products = Product.objects.filter(Q(title__contains=search_str) |
Q(description__contains=search_str))
... if consider that the visitor can search in title and content of
flat pages and in title and description of products.

To make the search even more flexible, you can parse the search_str
into chunks considering that spaces react as OR operators (like in the
popular search engines) and then to form some complex filter arguments
and pass them to the filter method using *args and **kwargs, like this
flatpage_args = ... # OR'ed arguments
flatpage_kwargs = ... # arguments to AND
flatpages = FlatPage.objects.filter(*flatpage_args, **flatpage_kwargs)
product_args = ... # OR'ed arguments
product_kwargs = ... # arguments to AND
products = Product.objects.filter(*product_args, **product_kwargs)

Regards,
Aidas Bendoraitis [aka Archatas]

Mary

unread,
Mar 18, 2007, 1:49:07 PM3/18/07
to Django users
Thank you for replying me
I tried flatpages =

FlatPage.objects.filter(Q(title__contains=search_str) |
Q(content__contains=search_str))
products = Product.objects.filter(Q(title__contains=search_str) |
Q(description__contains=search_str))
but it gave me error
Exception Type: NameError
Exception Value: global name 'Q' is not defined
If you can help me and tell what is the right syntax i ould really
appreciate that a lot

Thank you in advance
Mary Adel

On Feb 23, 1:37 pm, "Aidas Bendoraitis" <aidas.bendorai...@gmail.com>
wrote:


> Just filter searchable models according the searchable fields by the

> user-input. You'll need a form with a field forsearchstring. And a


> view for results with something like:
> flatpages = FlatPage.objects.filter(Q(title__contains=search_str) |
> Q(content__contains=search_str))
> products = Product.objects.filter(Q(title__contains=search_str) |
> Q(description__contains=search_str))

> ... if consider that the visitor cansearchin title and content of


> flat pages and in title and description of products.
>

> To make thesearcheven more flexible, you can parse the search_str


> into chunks considering that spaces react as OR operators (like in the

> popularsearchengines) and then to form some complex filter arguments


> and pass them to the filter method using *args and **kwargs, like this
> flatpage_args = ... # OR'ed arguments
> flatpage_kwargs = ... # arguments to AND
> flatpages = FlatPage.objects.filter(*flatpage_args, **flatpage_kwargs)
> product_args = ... # OR'ed arguments
> product_kwargs = ... # arguments to AND
> products = Product.objects.filter(*product_args, **product_kwargs)
>
> Regards,
> Aidas Bendoraitis [aka Archatas]
>

> On 2/22/07, Mary <madelghat...@gmail.com> wrote:
>
>
>
> > How can i add asearchfeature to my website?

doug.na...@gmail.com

unread,
Mar 18, 2007, 8:52:17 PM3/18/07
to Django users
> Exception Type: NameError
> Exception Value: global name 'Q' is not defined
You are missing:

from django.db.models import Q

lbolo...@gmail.com

unread,
Mar 19, 2007, 8:50:09 AM3/19/07
to Django users
On Feb 22, 11:07 pm, "Mary" <madelghat...@gmail.com> wrote:
> How can i add a search feature to my website?
>
> Can anybody help me in this

Hi Mary,

have a look at these two snippets:
http://www.djangosnippets.org/snippets/31/ (long version, easier to
understand the principle)
http://www.djangosnippets.org/snippets/32/ (compact version)

L.

Mary

unread,
Mar 20, 2007, 12:56:01 PM3/20/07
to Django users
Thanks for all the tips
I just want to know how the return result template will look like
So if any body can give me a small example i would really appretiate

Thank you in advance --
Mary Adel
:(

On Mar 19, 2:50 pm, "lbologn...@gmail.com" <lbologn...@gmail.com>
wrote:


> On Feb 22, 11:07 pm, "Mary" <madelghat...@gmail.com> wrote:
>

> > How can i add asearchfeature to my website?


>
> > Can anybody help me in this
>
> HiMary,
>

> have a look at these two snippets:http://www.djangosnippets.org/snippets/31/(long version, easier to

Derek Hoy

unread,
Mar 21, 2007, 6:09:58 PM3/21/07
to django...@googlegroups.com
Another option for search is to use the google co-op thing- look in
your google services. If your site is public, then it could be all
you need.

I've used it on 2 sites and they're non-commercial so I can switch off
the ads. It took some time for google's indexing to settle down, but
it seems to work fine now.

Derek

Reply all
Reply to author
Forward
0 new messages