views:

157

answers:

1

I want to create a search box in Django that is situated at the URL /search, and redirects to a friendly URL like /search/queryterm - rather than having ?q=queryterm in the URL)

So the user types in a query term, it takes them to /search/queryterm, and shows a list of results.

I can get and display the results OK given a URL like /search/queryterm.

But I don't know the best way to handle the URL redirect from the form. Should this be a redirect in pure HTML in the form (which feels slightly hacky), and if so, how can I implement this?

Or should the form POST to /search/, then ResponseRedirect to the /search/queryterm page? Wouldn't this be slower?

Thanks!

+5  A: 

POSTing a search query is a bad idea and a misuse of the HTTP verb for what ought to be a GET request, I think. Is there any reason you can not redirect to a friendly URL based on the request.GET parameters?

I also disagree with the principle of using "friendly" URLs for search queries; they should be used for permanent resources rather than something transient like search results, in my opinion.

The reason there has been a fightback of sorts for "friendly" URLs is because querystrings were being abused for accessing things that should exist at plain URLs, like /product.php?id=35. I don't think search results fall into this category.

Ben James
Thanks very much for the comprehensive answer. Out of interest, why is POSTing a search query a bad idea?I will definitely the request.GET parameters now that I know it's possible (bit of an HTTP novice). Thanks again.
AP257
+1. Search is precisely the sort of case for which querystrings _should_ be used. Creating URLs willy-nilly for transient resources is abuse of URLs.
Carl Meyer