问题
In dealing with forms with multiple input parameters and pagination, where user can choose any number of parameters (like a search form) which approach is better and why? receiving data straight from request.GET in one view or implementing a Post/Redirect/Get (2 views).
using Post/Redirect/Get, one can easily sanitize data using ModelForms builtin methods:
if request.method == 'POST':
form = MySearchForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
#do stuff with cd
How would you sanitize data using request.GET?
Why would you want to use request.GET instead of POST/Redirect/GET in such circumstances?
回答1:
That depends on what your form does. If you're creating/modifying data, use POST. Otherwise, use GET (see http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods).
You can still use form.cleaned_data
with GET, you just have to pass request.GET
to the form.
来源:https://stackoverflow.com/questions/28939537/request-get-vs-request-post-redirect-get-in-django