Django 表单和过滤
所以我有这个作业选择表单,应该允许用户:
1)选择一组单选按钮的过滤器。这允许用户选择他想要过滤列表的方法。 (这将使用“filterBy”查询字符串 2 执行 GET
)允许用户通过来自 comobox 的对象(即 #1 的结果)过滤列表。此表单执行 POST,并将“filter”设置为 filterBy 对象的 pk
以下是我的代码:
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
elif 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')
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
elif 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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
request.REQUEST
(请参阅:Django 请求和响应对象)。它包含request.POST
和request.GET
的值。另外,在 Python 中使用字典时,您应该始终验证您尝试访问的密钥是否存在。这可以通过以下两种方式之一完成:
1) 显式测试字典中的键
2) 使用字典的
get
方法使用
dict.get
您还可以传递默认值:Use
request.REQUEST
(see: Django Request and Response Objects). It holds the values of bothrequest.POST
andrequest.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
2) Use the dictionary's
get
methodWith
dict.get
you can also pass a default value:您应该将有问题的代码放在表单的重写
__init__
方法中。You should place your problematic code in form's overrided
__init__
method.