Django-基于GET参数在搜索表单上设置复选框的更好方法

Django-基于GET参数在搜索表单上设置复选框的更好方法,django,django-forms,Django,Django Forms,我已经做了大量的搜索,但是似乎在任何地方都没有提到这种特殊的表单创建品牌 我正在创建一个搜索页面,查询数据库中属性等于或高于给定阈值的所有元素集。现在我有一个简单的表单,它有5个属性,每个属性有5个阈值,每个阈值都有自己的复选框 attrib_1 X threshold 1 X threshold 2 X threshold 3 X threshold 4 X threshold 5 attrib_2 X threshold 1 X threshold 2 X threshold 3 X thr

我已经做了大量的搜索,但是似乎在任何地方都没有提到这种特殊的表单创建品牌

我正在创建一个搜索页面,查询数据库中属性等于或高于给定阈值的所有元素集。现在我有一个简单的表单,它有5个属性,每个属性有5个阈值,每个阈值都有自己的复选框

attrib_1 X threshold 1 X threshold 2 X threshold 3 X threshold 4 X threshold 5
attrib_2 X threshold 1 X threshold 2 X threshold 3 X threshold 4 X threshold 5  
... etc ...
HTML的外观如下所示:

  <div class= "form-inline">
    <label>Attribute 1</label>
    <label class="checkbox inline">
      <input type="checkbox" name="attrib 1" value="1"> Very Negative
    </label>
    <label class="checkbox inline">
      <input type="checkbox" name="attribt 1" value="2"> Negative
    </label>
    <label class="checkbox inline">
      <input type="checkbox" name="attrib 1" value="3"> Nonfactor
    </label>
    <label class="checkbox inline">
      <input type="checkbox" name="attrib 1" value="4"> Positive
    </label>      
    <label class="checkbox inline">
      <input type="checkbox" name="attrib 1" value="5"> Very Positive
    </label>
  </div>

属性1
非常消极
消极的
非因子
肯定的
非常积极
然后,我使用GET参数中的信息搜索数据库。当我显示搜索结果时,有什么优雅的方法可以确保复选框反映搜索查询?我希望用户选中一些框,查看结果,然后再选中一些框来优化搜索,我不希望他们每次提交搜索时都要重新检查所有框

我已经考虑了一些方法来做到这一点。我可以对每个复选框使用if/else语句,并适当地填写checked属性。这会起作用,但看起来不雅观,不太干燥,并且会导致一个非常复杂的模板。或者,在视图中,我可以创建一个数据结构(dict of list或list of tuple,可能是dict of list),其中每个复选框都有“checked”或空字符串。这将产生一个更干净的模板,但我怀疑有一种更合适的Django/Pythonic方法可以做到这一点。我也考虑过一种定制的形式,但这似乎像是试图把一个方形的钉子装进一个圆形的孔中


那么,有什么优雅的方法可以确保搜索表单上的复选框根据GET参数被正确选中呢?

我假设您是通过刷新而不是AJAX发回页面的。在这种情况下

我将假设(根据Django标准),您已经将所有这些复选框作为Django表单的一部分。在这种情况下,您可以向表单传递一系列参数(我建议使用字典),其中包含所有复选框的初始值

class SearchQuery(forms.form)

#Adding an init will allow us to pass arguments to this form In
# This case, a single dictionary argument named 'context'
    def __init__(self, *args, **kwargs)
        checkbox_context = kwargs.pop('context')
        super(SearchQuery,self).__init__(*args, **kwargs)
        #Now, instead of doing a bunch of if statements, we can say that
        # our dictionary passed a series of True and False keys that will
        # tell us how our checkboxes should be, in their initial state
        self.fields['checkbox_one'].initial = context['box1']

    checkbox_one = forms.BooleanField()

因此,假设我们传递了
context={'box1':True}
,那么我们的复选框将被呈现为初始值'True'或'Checked'

我假设您是通过刷新而不是AJAX发回页面。在这种情况下

我将假设(根据Django标准),您已经将所有这些复选框作为Django表单的一部分。在这种情况下,您可以向表单传递一系列参数(我建议使用字典),其中包含所有复选框的初始值

class SearchQuery(forms.form)

#Adding an init will allow us to pass arguments to this form In
# This case, a single dictionary argument named 'context'
    def __init__(self, *args, **kwargs)
        checkbox_context = kwargs.pop('context')
        super(SearchQuery,self).__init__(*args, **kwargs)
        #Now, instead of doing a bunch of if statements, we can say that
        # our dictionary passed a series of True and False keys that will
        # tell us how our checkboxes should be, in their initial state
        self.fields['checkbox_one'].initial = context['box1']

    checkbox_one = forms.BooleanField()

假设我们传递了
context={'box1':True}
,那么我们的复选框将以'True'或'Checked'的初始值呈现。

我是Django新手,没有意识到所有表单都是Django表单是Django标准。是这样吗?似乎是因为表单非常不标准(只是复选框,每个都有一个组标签),Django的表单可能不值得麻烦。也许不是“标准”,但它们是一个功能强大得难以置信的工具,当你可以让Django为你做所有的事情时,编写所有的HTML似乎很愚蠢。我是Django的新手,没有意识到所有表单都是Django表单是Django的标准。是这样吗?似乎是因为表单非常不标准(只是复选框,每个都有一个组标签),Django的表单可能不值得麻烦。也许不是“标准”,但它们是一个功能强大得难以置信的工具,当你可以让Django为你做这一切时,编写所有HTML似乎很愚蠢。