Form redirect to URL containing query term? - pure HTML or Django

送分小仙女□ 提交于 2019-12-25 00:52:35

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/1860600/form-redirect-to-url-containing-query-term-pure-html-or-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!