I am implementing a search form in Django. I can do a POST or a GET request. Each has their use-cases (POST request, if I want to change data on the server, GET request, if I just want to get data from the server).
- Via POST request (the search keywords are not visiable in the URL, but rather in a dict in request.POST); cons: I cannot bookmark the search
- via GET request (the search keywords are visible in the URL, for intance
localhost:8000/books/?author=schultz
); cons(?): the part?author=schultz
cannot be processed by the URL handler (see [2] below). I need to read the data fromrequest.GET.get("author", None)
in my view function. - or directly in the URL like so:
localhost:8000/books/search/author/schultz
?
The author in [1] says, that Django's preferred way to handle a URL is not via GET (like so: /category_check_view/?item_id=2
, but rather like so /category_check_view/2
)
If I would like to implement search like this: localhost:8000/books/author/schultz
, then I would have to process a GET request, read the params ?author=schultz
via request.GET.get("author", None)
and in my view do a redirect from this URL localhost:8000/books
(in which I have a form and a GET request) to this localhost:8000/books/author/schultz
.
Does this approach make sense? Or am I overcomplicating things? Just leave it at a GET request to implement my search form?
[1] Yuval Adam says in this post that
GET params are not processed by the URL handler, but rather passed directly to the GET param dict accessible in a view at request.GET.
The Django (i.e. preferred) way to do handle URLs is the first one.