Still curious about the answer, but I've realized I can accomplish
what I want by using a URL pattern
to pass the data from the URL into the view that way.

On Jan 30, 4:15 pm, adrian <adrian...@gmail.com> wrote:
> My question is about how to read form items from the POST request
> before the form is validated.
> I try to read request.POST['myKey'] and Python says
> MultiValueDictKeyError: "Key 'myKey' not found in <QueryDict: {}> even
> though Firebug shows the POST request does contain that key.
>
> Here's why I want to do this.   I've got a page with a form that is
> submitted via javascript in two different ways.
>
> 1.  when user changes the value of a dropdown box, an onchange
> function submits the form
> with the dropdown form field set and one other field set to a special
> code.   The goal
> is to reload the page with different data in one area.   (Note that I
> also tried doing this using a javascript replace or reload but there
> seems to be no way to pass any arguments to reconfigure the page - it
> is always a POST request so ignores GET additions to the URL.)
>
> 2.  when the user clicks somewhere  else, an onclick submits the form
> by Ajax which loads new data into a different area with no page
> refresh.  This submission is complete and needs validation.
>
> Here's my code (the Django side)
>
> def addtest(request):
>     now = datetime.datetime.now()
>     today = now.strftime("%m/%d/%Y")
>     region = 'WOR'   # default, may be overridden later
>
>     birdlist_area_selected = {}  # one item in this dict is set to
> "selected", used in template
>
>     if request.method == 'POST': # form submitted or ajax data initial
> load...
>
>         form = ImportedSightingForm(request.POST) # A form bound to
> the POST data
>         if request.POST['species_name'] == 'None':   # flag indicated
> javascript page reload for new region bird list
>
>             region = request.POST['birdlist_area']
>         elif form.is_valid(): # All validation rules pass
>
>             instance = form.save(commit=False)
>
>             instance.date_added =  now.strftime("%Y-%m-%d %H:%M:%S")
>
>             instance.user = request.user  # should be person related
> to user id of current user
>
>             new_instance = instance.save()
>             #flow through into view
>
>         else:  # initial ajax load or error
>             if 'loadAll' in request.POST: #ajax load
>                 logging.debug( "initial ajax load")
>             else:
>                 logging.debug( "form errors found:")
>                 logging.debug( form.errors)
>                 logging.debug( request.POST.items())
>
>     else: #method is GET: blank form, birdlist area may be set
>         logging.debug( "GET request (form reload)")
>         form = ImportedSightingForm() # An unbound form except keeps
> any filled field
>
>         #flows into remainder of file
>
>     #gets here for all requests, GET or  POST
>
>     query_set = ImportedSighting.objects.filter(  # all sightings by
> this user today
>                 user=request.user
>             ).filter(
>             date_added__gte=datetime.datetime(now.year, now.month,
> now.day)
>             ).order_by('date_added')
>     if request.is_ajax():
>         logging.debug( "ajax request")
>         # reach here in two situations:
>         # 1. datatable was just created and is loading initial data by
> ajax
>         # 2. user has added or deleted a sighting   (from their list)
>         if 'userAdd' in request.POST:
>             logging.debug( "userAdd set, getting item")
>             #adding a row
>             sighting_list = query_set.reverse()[:1]  # latest entry
> (single)
>             logging.debug( sighting_list)
>         else:
>             sighting_list = query_set  # all items
>
>         return HttpResponse(serializers.serialize("json",
> sighting_list), mimetype='application/javascript')
>
>     # sighting_list sent here will be hidden unless javascript
> unavailable
>     sighting_list = query_set
>     num_sightings = len(sighting_list)
>
>     if region == 'WOR':
>         bird_list = Species.objects.filter(
>             common_name__startswith='A'
>             ).order_by('common_name')
>     else:
>         bird_list = RegionSpecies.objects.filter(
>             region__exact=region
>             ).order_by('common_name')
>
>     return render_to_response('addtest.html', {
>                 'birdlist_area_selected': birdlist_area_selected,
>                 'num_sightings': num_sightings,
>                 'form': form,
>                 'today': today,
>                 'sighting_list': sighting_list,
>                 'bird_list': bird_list})
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to