Checking for content in Django request.POST

GrumpyCanuck picture GrumpyCanuck · Nov 17, 2009 · Viewed 20.9k times · Source

I am accepting data via request.POST like this:

if request.method == 'POST': 
    l = Location() 
    data = l.getGeoPoints(request.POST) 
    appid = settings.GOOGLE_API_KEY 

    return render_to_response('map.html',  
                               {'data': data, 'appid': appid}, 
                               context_instance=RequestContext(request)) 

It accepts data from a bunch of text input boxes called form-0-location all the way up to form-5-location.

What I want to add in is a check to make sure that request.POST contains data in any of those input fields. I think my problem is that I do not know the correct terminology for describing this in Django.

I know how to do it in PHP: look inside $_POST for at least one of those fields to not be empty, but I can't seem to find the right answer via searching for google.

If I don't find any data in those input fields, I want to redirect the user back to the main page.

Answer

Rishabh Manocha picture Rishabh Manocha · Nov 17, 2009

Have you thought about using Django's Forms?? You can mark fields as "required" when defining a form and Django will take care of validating if said fields have data in them upon submission. They also do other kinds of validation.