Search code examples
htmldjangoselectdefault

default select box by a passed parameter to django template?


I'm passing a dictionary to a django template and trying to set a default value for a select box. This template is an 'edit product' template for a previous 'add product' template, thus the user selected one of the following options from a select box when he added a new product and what I am trying to do now is in the 'edit product' template so he can change it while the default will be as the one he selected at first. Is there a way to do something like this:

                 <td><select name="gender_limit" deafault="{{django_context.gender}}">
                     <option value="Male">Male</option>
                     <option value="Female">Female</option>
                     <option value="Both">Both</option>
                    </select>
                 </td>

Rather than:

                 <td><select name="gender_limit" >
                     <option value="Male" selected >Male</option>
                     <option value="Female">Female</option>
                     <option value="Both">Both</option>
                    </select>
                 </td>

I have tried to look a solution for this but haven't found. Any help is appreciated. Thanks


Solution

  • I found a solution to my problem, here is an example (a different one from the question example):

    <select name="units">
      <option ng-selected="'{{ med.units }}' == 'mg' " value="mg">mg</option>
      <option ng-selected="'{{ med.units }}' == 'gr' " value="gr">gr</option>
      <option ng-selected="'{{ med.units }}' == 'mcg' " value="mcg">mcg</option>
      <option ng-selected="'{{ med.units }}' == 'ng' " value="ng">ng</option>
    </select>
    

    Or even as so:

    <select name="units" ng-model="'{{ med.units }}'">
      <option value="mg">mg</option>
      <option value="gr">gr</option>
      <option value="mcg">mcg</option>
      <option value="ng">ng</option>
    </select>
    

    med.units is django context passed from a view.