开发者

Django Forms - Processing GET Requests

We have an existing Django form that accepts GET requests to allow users to bookmark their resulting query parameters. The form contains many fields, most of which开发者_Go百科 are required. The form uses semi-standard boilerplate for handling the request, substituting GET for POST:

if request.method == 'GET':
   form = myForm(request.GET)
   if form.isValid()
      # Gather fields together into query.
else
   form = myForm()

The problem is that the first time the form is loaded, there's nothing in the GET request to populate the required fields with, so most of the form lights up with 'missing field' errors.

Setting initial values doesn't work; apparently, the non-existent values in the GET request override them.

How can we avoid this? I'm pretty certain we're simply not processing things correctly, but I can't find an example of a form that handles GET requests. We want errors to show up if the user hits the "Submit" button while fields are blank or otherwise invalid, but don't want these errors showing up when the form is initially displayed.


The positional argument to the forms.Form subclass informs Django that you intend to process a form rather than just display a blank/default form. Your if request.method == 'GET' isn't making the distinction that you want because regular old web requests by typing a URL in a web browser or clicking a link are also GET requests, so request.method is equal to GET either way.

You need some differentiating mechanism such that you can tell the difference between a form display and a form process.

Ideas:

If your processing is done via. AJAX, you could use if request.is_ajax() as your conditional.

Alternatively, you could include a GET token that signifies that the request is processing. Under this example, first you'd need something in your form:

<input type="hidden" name="action" value="process_form" />

And then you can look for that value in your view:

if 'action' in request.GET and request.GET['action'] == 'process_form':
    form = myForm(request.GET)
    if form.is_valid():
        # form processing code
else:
    form = myForm()

I'll also give you the standard, boilerplate point that it's generally preferable not to use GET for form processing if you can help it (precisely because you run into difficulties like this since you're using an anomalous pattern), but if you have a use case where you really need it, then you really need it. You know your needs better than I do. :-)


If your clean page load doesn't have any non form GET params, you can differentiate between a clean page load and a form submit in your view. Instead of the usual

form = YourForm()
if request.POST:

you can do

if request.GET.items():
    form = YourForm(request.GET)
    if form.is_valid():
        ...

else:
    form = YourForm()

If your clean page load could have other params (eg email link tracking params) you'll need to use the QueryDict methods to test if any of your form params are in the request.


request.GET is and empty dictionary when you first load a clean form. Once you have submitted the form, request.GET will be populated with your fields data, even if the fields contain only empty data.


My first question is this, which I posted as comment: Why not just use request.POST and the standard way of processing form data?

After considering everything here, perhaps what you are looking for is a way of processing data in your query string to populate a form. You can do that without using request.GET as your form.data.

In my own views, I take advantage of a utility function I created to add initial data to the form from request.GET, but I am not going to share that function here. Here's the signature, though. initial_dict is typically request.GET. model_forms is either a single ModelForm or a list of ModelForm.

def process_initial_data(model_forms, initial_dict):

Nevertheless, I am able to process the form through the standard practice of using request.POST when the form is POSTed. And I don't have to pass around all kinds of information in the URL query string or modify it with JavaScript as the user enters information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜