Search Field on All Pages

159 views
Skip to first unread message

wchildsuk

unread,
Aug 8, 2010, 5:41:15 AM8/8/10
to Django users
Hi,

I want a search field on all my pages and was wondering the best way
to do this. I could create a function and import it to every view but
this doesn't seem to follow the django DRY principles.

I was wondering if anyone had any advice on how to best do this with
django?

Thank in advance

Wes

Tim Sawyer

unread,
Aug 8, 2010, 8:04:23 AM8/8/10
to django...@googlegroups.com
What I do is to setup a search that works at /search/q=search_term, and
then create a form on each page that submits to /search.

<form action="/search/" method="get"><input type="text" name="q"/>
<input type="submit" value="Search"></form>

This form is in my top level site template. The /search/ url is part of
a search application.

There's an example of this working at http://www.brassbandresults.co.uk/

Hope that helps,

Tim.

Steve Holden

unread,
Aug 8, 2010, 8:15:12 AM8/8/10
to django...@googlegroups.com
The usual way would be to use template inheritance. Put the search form
in your base template and have your remaining templates extend that base
template.

http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

regards
Steve
--
I'm no expert.
"ex" == "has-been"; "spurt" == "drip under pressure"
"expert" == "has-been drip under pressure".

Message has been deleted
Message has been deleted

Roald de Vries

unread,
Aug 8, 2010, 10:07:42 AM8/8/10
to django...@googlegroups.com
On Aug 8, 2010, at 2:04 PM, Tim Sawyer wrote:
> What I do is to setup a search that works at /search/q=search_term,
> and then create a form on each page that submits to /search.
>
> <form action="/search/" method="get"><input type="text" name="q"/>
> <input type="submit" value="Search"></form>
>
> This form is in my top level site template. The /search/ url is
> part of a search application.
>
> There's an example of this working at http://www.brassbandresults.co.uk/
>
> Hope that helps,
>
> Tim.
>
> On 08/08/10 10:41, wchildsuk wrote:
>> Hi,
>>
>> I want a search field on all my pages and was wondering the best way
>> to do this. I could create a function and import it to every view but
>> this doesn't seem to follow the django DRY principles.

Personally, I like all of my forms to be classes. If you want that,
you should create a context processor.

http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

Cheers, Roald

Tim Sawyer

unread,
Aug 8, 2010, 12:45:31 PM8/8/10
to django...@googlegroups.com
On 08/08/10 14:48, kostia wrote:
> To continue, Tim, do you use haystack? I passed its beginning tutorial
> and configured with xapian through haystack-xapian module. Now I'm
> discovering the other haystack docs and api. Not everything is clear.
> For example I have a user with name Bob. When I type in the search
> "ob", it does not return the result, only if I type "Bob", or "bob".

Not on http://www.brassbandresults.co.uk/. The search field there
searches just the name field of various model objects so it's easier to
just do model selects.

Here's an article I wrote after using Haystack and Whoosh to improve the
search on http://www.drumcoder.co.uk/, if that helps.

http://drumcoder.co.uk/blog/2010/may/19/django-text-search-haystack-and-whoosh/

Tim.


André Santos

unread,
Aug 8, 2010, 1:14:49 PM8/8/10
to django...@googlegroups.com
What about creating a template_tag for the form? Is that a good way to add it to several templates?

2010/8/8 Roald de Vries <down...@gmail.com>


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
André Santos Teixeira de Carvalho
Undergraduating in Computer Science DCC/UFRJ
Message has been deleted

Wesley Childs

unread,
Aug 9, 2010, 3:40:53 AM8/9/10
to django...@googlegroups.com
Thanks for everyone's advice.

My final solution will be to use template inheritance to have the same form across the site and to use the action="/search" to ensure I don't have to import a search function into every single view.

Thanks again

Wes

On 8 August 2010 19:36, kostia <kostya....@gmail.com> wrote:
Yes, somebody can use template tag, somebody content processor and the
most intuitive solution is to use template inheritance, as was
suggested.

Thank you, Tim. Wish you good luck. I loved your musician web site.
Reply all
Reply to author
Forward
0 new messages