2016-12-05 75 views
0

我正在構建類似於this question中的選項卡式結構。我有一組學生,並且我想要一個模式內的選項卡,可以選擇一些表格作爲Section對象的成員。Bootstrap 3製表符和Django表單

我有選項卡,並有一個按等級過濾的表單,但當我單擊選項卡時,我無法將等級變量獲取到表單中以提供過濾。如果我正在重新加載頁面,我可以弄清楚,或者即使我創建了15種不同的表單(每個年級一個表格),然後單獨調用它們,但這似乎是一種可怕的方式。

如何在標籤切換時呈現表單,同時將選項卡的名稱(等級)傳遞給表單?只要我將等級信息硬編碼到表單中,一切都可以正常工作。

forms.py

class RosterAddForm(forms.ModelForm): 
    class Meta: 
     model = Section 
     fields = ['students'] 
    def __init__(self, *args, **kwargs): 

     details = kwargs.pop('details',None) 
     this_section = details['this_section'] 
     roster = this_section.students.all() 
     super(RosterAddForm, self).__init__(*args, **kwargs) 
     self.fields['students'].widget = forms.CheckboxSelectMultiple() 
     try: 
      self.fields['students'].queryset = Student.objects.filter(Grade="11") 

這個硬編碼的問題:需要這是通過選定的選項卡

  self.fields['students'].initial = roster 
     except: 
      pass 

views.py

... 
     details = {'this_section':this_section, 'roster':roster} 
     rosteraddform = RosterAddForm(None,prefix='rosteradd',details=details) 

     context = {'this_teacher':this_teacher,'coursezip':coursezip,'this_course':this_course, 
        'this_section':this_section, 'this_admin':this_admin, 'adminzip':adminzip, 
        'roster':roster, 'rosteraddform':rosteraddform, 'grades':["3-4","PK","K","1","2","3","4","5","6","7","8","9","10","11","12"], 
        } 

     return render(request,'benchmarks/teacherindex.html', context) 

    elif request.method == "POST": 

     this_section = Section.objects.get(pk=Section_id) 
     roster = this_section.students.all() 

     details = {'this_section':this_section, 'roster':roster} 
     rosteraddform = RosterAddForm(request.POST,prefix='rosteradd',details=details) 

     if rosteraddform.is_valid(): 
      #a list of the pk's of chosen students 
      chosen_students = rosteraddform.cleaned_data['students'] 
      this_section.students.clear() 
      for kid in chosen_students: 
       this_section.students.add(kid) 

      return redirect(reverse('benchmarks:indexteacher', kwargs={'Section_id':Section_id,'Teacher_id':Teacher_id})) 
     else: 
      return render(request, template_name, {'form':form}) 

HTML

!-- Roster add modal --> 

<div class="modal fade" id="RosterAddModal" role="dialog" > 
<div class="modal-dialog" style="width:900px;"> 

    <!-- Modal content--> 
    <div class="modal-content"> 
    <div class="modal-header" style="padding:5px 10px;"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4>Add to Roster</h4> 
    </div> 
    <div class="modal-body" style="padding:10px 10px;"> 
    <!-- Nav tabs --> 
    <ul class="nav nav-tabs" role="tablist" > 
     {% for grade in grades %} 
     <li role="presentation"><a href="#{{ grade }}" aria-controls={{ grade }} role="tab" data-toggle="tab">{{ grade }}</a></li> 
     {% endfor %} 
    </ul> 
    <!-- Tab panes --> 
    <div class="tab-content" style="background:#EEEEEE; max-height:1000px; min-height:400px;" > 

    {% for grade in grades %} 
    <div role="tabpanel" class="tab-pane " id="{{ grade }}" style="padding:15px;"> 
     {{ grade }} 
    <form data-parsley-validate method="post" id="rosteraddform" action="" enctype="multipart/form-data" 
       data-parsley-trigger="focusout"> 

     {% csrf_token %} 

     {{ rosteraddform.as_p }} 


     <p id="login-error"></p> 

     <input type="submit" class="btn submit" name="RosterAdd" value="Add Students" /> 
     </form> 
    </div> 
    {% endfor %} 
</div> 
</div> 
    <div class="modal-footer"> 
    </div> 
    </div> 

</div> 
</div> 

回答

0

這裏的解決方案,這是不完美的,是提前渲染的時間形式的每個版本:

details = {'this_section':this_section, 'roster':roster} 

    grades = ["3-4","PK","K","1","2","3","4","5","6","7","8","9","10","11","12"] 
    rosteraddforms = [] 
    for grade in grades: 
     rosteraddforms.append(RosterAddForm(None,prefix='rosteradd'+grade,details=details,grade=grade)) 
    gradezip = zip(grades,rosteraddforms) 

在模板,創建標籤時通過壓縮迭代。

在用於POST視圖:

if any(key.startswith("RosterAdd") for key in request.POST): 
     print "roster add" 
     this_section = Section.objects.get(pk=Section_id) 
     roster = this_section.students.all() 

     details = {'this_section':this_section, 'roster':roster} 
     rosteraddforms = [] 
     grades = ["3-4","PK","K","1","2","3","4","5","6","7","8","9","10","11","12"] 
     for grade in grades: 
      rosteraddforms.append(RosterAddForm(request.POST, prefix = 'rosteradd'+grade,details=details)) 
     #print request.POST 

     for rosteraddform in rosteraddforms: 
      grade = rosteraddform.prefix[9:] 

      if 'RosterAdd'+grade in request.POST: 
       rosteraddformused = rosteraddform 
       if rosteraddform.is_valid():