title: Django - Better way to set checked checkboxes on a search form based on GET parameters
body: <p>I've done a good bit of searching, but it doesn't seem like this particular brand of form creation is addressed anywhere.</p>
  
  <p>I'm creating a search page that queries the database for the set of all elements which have attributes at or above a given threshold. Right now I have a simple form that has 5 attributes, which have 5 thresholds each, each with their own checkbox i.e.</p>
  
  <pre><code>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 ...
  </code></pre>
  
  <p>The HTML looks something like this:</p>
  
  <pre><code>  &lt;div class= "form-inline"&gt;
      &lt;label&gt;Attribute 1&lt;/label&gt;
      &lt;label class="checkbox inline"&gt;
        &lt;input type="checkbox" name="attrib 1" value="1"&gt; Very Negative
      &lt;/label&gt;
      &lt;label class="checkbox inline"&gt;
        &lt;input type="checkbox" name="attribt 1" value="2"&gt; Negative
      &lt;/label&gt;
      &lt;label class="checkbox inline"&gt;
        &lt;input type="checkbox" name="attrib 1" value="3"&gt; Nonfactor
      &lt;/label&gt;
      &lt;label class="checkbox inline"&gt;
        &lt;input type="checkbox" name="attrib 1" value="4"&gt; Positive
      &lt;/label&gt;      
      &lt;label class="checkbox inline"&gt;
        &lt;input type="checkbox" name="attrib 1" value="5"&gt; Very Positive
      &lt;/label&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>I then search the database using the information in the GET parameters. When I display the search results, what's an elegant way to make sure the checkboxes reflect the search query? I expect that the user will check some boxes, look at the results, then check some more to refine the search and I don't want them to have to recheck all the boxes each time they submit a search. </p>
  
  <p>I've considered a few way to do this. I could use if/else statements for each checkbox and fill in the checked attribute appropriately. This would work, but seems inelegant, not very DRY, and would result in a very complex template. Alternatively, in the view I could create a data structure (dict of lists or list of tuples, probably dict of lists), which would have either 'checked' or the empty string for each checkbox. This would result in a cleaner template, but I suspect there's a more properly Django/Pythonic way to do this. I also considered a custom form, but that seemed like trying to fit a square peg in a round hole.</p>
  
  <p>So, what's an elegant way to make sure that check boxes on a search form are properly checked based on GET parameters?</p>