在管理页面中,它可以轻松完成,但是在查看页面中似乎存在问题。我已经尝试过但是失败了很多次。因为我是django的新手。如何在“自定义视图”页面中实现这种类型的表单集? (同时创建和更新) 我将jQuery插件用于动态FormSet。这是我在views.py
中的代码class ExamAdd(CreateView):
model = Exam
fields = ['title', 'examRoom']
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
data = super(ExamAdd, self).get_context_data(**kwargs)
print('This is data for exam add',data)
if self.request.POST:
data['dateformset'] = ExamDateFormSet(self.request.POST)
data ['shiftformset'] = ExamShiftFormSet(self.request.POST)
else:
data['dateformset'] = ExamDateFormSet()
data['shiftformset'] = ExamShiftFormSet()
return data
def form_valid(self, form):
context = self.get_context_data()
dateformset = context['dateformset']
shiftformset = context ['shiftformset']
with transaction.atomic():
self.object = form.save()
if dateformset.is_valid() and shiftformset.is_valid():
dateformset.instance = self.object
shiftformset.instance = self.object
instances = dateformset.save(commit = False)
for instance in instances:
instance.save()
instances = shiftformset.save(commit = False)
for instance in instances:
instance.save()
invigilatorAssignment = InvigilatorAssignmentForm()
return super(ExamAdd, self).form_valid(form)
class ExamUpdate(UpdateView):
model = Exam
fields = ['title', 'examRoom']
success_url = reverse_lazy('index')
examDateFormSet = inlineformset_factory(Exam,ExamDate,form = ExamDateAddForm,formset=BaseExamDateEditFormSet2,extra = 1,can_delete=True)
examShiftFormSet = inlineformset_factory(Exam,Shift,form = ExamShiftAddForm,formset = BaseShiftEditFormSet2,extra = 1,can_delete=True)
def get_context_data(self, **kwargs):
data = super(ExamUpdate, self).get_context_data(**kwargs)
if self.request.POST:
data['dateformset'] = self.examDateFormSet(self.object,self.request.POST)
data ['shiftformset'] = self.examShiftFormSet(self.object,self.request.POST)
else:
data['dateformset'] = self.examDateFormSet(self.object)
data['shiftformset'] = self.examShiftFormSet(self.object)
print ("This is data",data)
print ("THis is objetc",self.object)
return data
def form_valid(self, form):
context = self.get_context_data()
dateformset = context['dateformset']
shiftformset = context ['shiftformset']
with transaction.atomic():
self.object = form.save()
if dateformset.is_valid() and shiftformset.is_valid:
if dateformset.has_changed():
dateformset.instance = self.object
dateformset.save()
if shiftformset.has_changed():
shiftformset.instance = self.object
shiftformset.save()
invigilatorAssignment = InvigilatorAssignmentForm()
else:
print ("DateFormSet Not Valid",dateformset.errors)
print ("ShiftFormSet Not Valid",shiftformset.errors)
return super(ExamUpdate, self).form_valid(form)
和模板中。
{% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block title %}{% endblock %}
{% block mainContent %}
<style>
</style>
<h2>Add Exam</h2>
<hr>
<div class="col-md-4">
<form action = '' method="post" >{% csrf_token %}
{%include 'includes/form.html'%}
<table class="table">
{{ dateformset.management_form|crispy }}
{% for form in dateformset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %} dateformset formset_row">
{% for field in form.visible_fields %}
<td>
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<table class="table">
{{ shiftformset.management_form }}
{% for form in shiftformset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} shiftformset formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Save"/> <a href="{% url 'index' %}">Back to the list</a>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'formset/jquery.formset.js' %}"></script>
<script type="text/javascript">
$('.dateformset').formset({
addText: 'Add Date',
deleteText: 'Delete',
prefix: 'examdates'
});
$('.shiftformset').formset({
addText: 'Add Shifts',
deleteText: 'Delete',
prefix: 'shifts'
});
</script>
{% endblock %}
</body>
</html>
这是我为此项目的仓库。 https://github.com/regmirabindra/Invigilator-Management