django forms and filtering
So i have this jobs select form that should allow a user to:
1) select a filterBy set of radio buttons. This lets the user select which method he wants to filter a list by. (this will perform a GET with a 'filterBy' query string
2) allow a user to filter the list by an object from a comobox that is the result of #1. This form performs a POST with the 'filter' set to the pk of a filterBy object
Here is the code i have:
selectForm = JobSelectForm()
filterByForm = FilterByForm()
filterForm = FilterForm()
if request.method == 'POST':
#this works just fine
if 'job' in request.POST:
return HttpResponseRedirect("/portal/jobs/%s/"%(request.POST['job']))
if 'filter' in request.POST:
filterForm = FilterForm(initial = {'filter': request.POST['filter']})
###### The Problem is below here
###### I cant get the 'filterBy' query string
###### because this is a POST and not a GET
###### Is there a better way to write this filtering?
if request.GET['filterby'] == 'G':
obj = Group.objects.get(pk=request.POST['filter'])
selectForm.fields['job'].queryset = Job.objects.filter(group=obj).order_by('name')
elif request.GET['filterby'] == 'H':
obj = Host.objects.get(pk=request.POST['filter'])
selectForm.fields['job'].queryset = Job.objects.filter(host=obj).order_by('name')
elif request.GET['filterby'] == 'L':
obj = Location.objects.get(pk=request.POST['filter'])
selectForm.fields['job'].queryset = Job.objects.filter(colo=obj).order_by('name')
###### All of this works fine too
eli开发者_开发百科f request.method == 'GET':
if request.GET.has_key('filterby'):
if request.GET['filterby']:
filterByForm = FilterByForm(initial = {'filterby': request.GET['filterby']})
if request.GET['filterby'] == 'G':
filterForm.fields['filter'].queryset = Group.objects.all().order_by('name')
elif request.GET['filterby'] == 'H':
filterForm.fields['filter'].queryset = Host.objects.all().order_by('name')
elif request.GET['filterby'] == 'L':
filterForm.fields['filter'].queryset = Location.objects.all().order_by('name')
Use request.REQUEST
(see: Django Request and Response Objects). It holds the values of both request.POST
and request.GET
.
Also, when using dictionaries in Python, you should always verify that the key you're trying to access exists. This can be done in one of two ways:
1) Explicitly test for key in dictionary
if request.GET.has_key('filterby') and request.GET['filterby'] == 'G':
obj = Group.objects.get(pk=request.POST['filter'])
selectForm.fields['job'].queryset = Job.objects.filter(group=obj).order_by('name')
2) Use the dictionary's get
method
if request.GET.get('filterby') == 'G':
obj = Group.objects.get(pk=request.POST['filter'])
selectForm.fields['job'].queryset = Job.objects.filter(group=obj).order_by('name')
With dict.get
you can also pass a default value:
if request.GET.get('filterby', valueIfKeyDoesntExist) == 'G':
obj = Group.objects.get(pk=request.POST['filter'])
selectForm.fields['job'].queryset = Job.objects.filter(group=obj).order_by('name')
You should place your problematic code in form's overrided __init__
method.
精彩评论