2016-12-05 11 views
0

this questionのタブ付き構造に似たものを構築しています。私には生徒がいます。セクションのオブジェクトのメンバーとしてそれらのいくつかを選択できるように、フォームをプルするモーダル内のタブが必要です。ブートストラップ3タブとDjangoフォーム

私はタブを持っており、等級別にフィルタリングするフォームを持っていますが、タブをクリックしたときにフィルタリングするためにグレード変数をフォームに入れることができません。もし私がページをリロードしていたら、それを理解することができました。あるいは、15の異なるフォーム(各グレードに1つずつ)を作成して別々に呼び出すこともできましたが、それはそれを行うためのひどい方法のようです。

フォームにタブ(グレード)の名前を渡しながら、タブの切り替え時にフォームをレンダリングするにはどうすればよいですか?グレード情報をフォームにハードコードする限り、すべてが完璧に機能します。

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) 

、タブを作成するときに郵便番号を反復します。

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(): 
POSTためのビューで

関連する問題