[Answered ]-Django Templates and drop-down list

2👍

Since your question was only directed to form rendering . . .

In the view:

class DefaultSquadsViewCreate(CreateView):
    template_name = 'fafl/defaultSquad_form.html'
    model = DefaultSquads
    fields = ['player', 'position_code', 'playing_status_code']
    success_url = reverse_lazy('fafl:defaultSquads-list')

    def get_context_data(self, **kwargs):
        context = super(DefaultSquadsView, self).get_context_data(**kwargs)
        context['clubs'] = Clubs.objects.all().order_by('club_id')
        context['players'] = Players.objects.all().order_by('player_id')
        context['positions'] = Positions.objects.all()
        return context

In the template:

    <div class="form-group">
      <label for="{{ form.club.id_for_label }}" class="col-sm-2 control-label">Club</label>
      <div class="col-sm-10">    
        <select id="{{ form.club.id_for_label }}" name="{{ form.club.html_name }}" class="form-control">
          <option value="" selected>None</option>
          {% for club in clubs %}
          <option value="{{ club.club_id }}">{{ club.nickname }}</option>
          {% endfor %}
        </select>    
      </div>
    </div>
    <div class="form-group">
      <label for="{{ form.player.id_for_label }}" class="col-sm-2 control-label">Player</label>
      <div class="col-sm-10">    
        <select id="{{ form.player.id_for_label }}" name="{{ form.player.html_name }}" class="form-control">
          <option value="" selected>Please select a player</option>
          {% for player in players %}
          <option value="{{ player.player_id }}">{{ player.display_name }}</option>
          {% endfor %}
        </select>    
      </div>
    </div>

    <div class="form-group">
      <label for="{{ form.postion_code.id_for_label }}" class="col-sm-2 control-label">Position Code</label>
      <div class="col-sm-10">    
        <select id="{{ form.position_code.id_for_label }}" name="{{ form.position_code.html_name }}" class="form-control">
          <option value="" selected>Please select a Position</option>
          {% for position in positions %}
          <option value="{{ position.position_code }}">{{ position.name }}</option>
          {% endfor %}
        </select>    
      </div>
    </div>
👤2ps

Leave a comment