Ich baue etwas ähnlich der Tabbed-Struktur in this question. Ich habe eine Gruppe von Schülern, und ich möchte Tabs in einem Modal, die ein Formular hochziehen, damit ich einige von ihnen als Mitglieder des Section-Objekts auswählen kann.Bootstrap 3 Tabs und Django Forms
Ich habe die Registerkarten, und haben ein Formular, das nach Grad gefiltert, aber ich kann nicht erhalten die Grad-Variable in das Formular, um die Filterung, wenn ich auf die Registerkarte klicken. Wenn ich die Seite neu laden würde, könnte ich das herausfinden oder auch wenn ich 15 verschiedene Formen (eine für jede Klasse) erstellt und sie einzeln aufgerufen hätte, aber das scheint eine schreckliche Art zu sein.
Wie rende ich das Formular beim Wechseln der Registerkarte, während Sie den Namen der Registerkarte (die Note) an das Formular übergeben? Alles funktioniert perfekt, solange ich die Noteninformation fest in das Formular einfüge.
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")
diese Hartcodierung ist das Problem: Notwendigkeit dieser
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
über ausgewählte Registerkarte sein!-- 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">×</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>