Django 表单和过滤

发布于 2024-11-08 20:24:43 字数 2062 浏览 0 评论 0原文

所以我有这个作业选择表单,应该允许用户:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港小镇 2024-11-15 20:24:43

使用 request.REQUEST (请参阅:Django 请求和响应对象)。它包含 request.POSTrequest.GET 的值。

另外,在 Python 中使用字典时,您应该始终验证您尝试访问的密钥是否存在。这可以通过以下两种方式之一完成:

1) 显式测试字典中的键

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) 使用字典的 get 方法

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')

使用 dict.get 您还可以传递默认值:

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')

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')
赏烟花じ飞满天 2024-11-15 20:24:43

您应该将有问题的代码放在表单的重写 __init__ 方法中。

You should place your problematic code in form's overrided __init__ method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文