我有一份报告表单report.html:
<select id="select" class="form-control" name="organization">
<option value="">-- Select Organization --</option>
{% for organization in organization_list %}
<option value="{{ organization.name }}">{{ organization.name }}</option>
{% endfor %}
</select>
提交此表单后,不会保存所选组织。但如果组织将被展示会更好。它对用户有用:她/他可以看到,哪些过滤器显示这些结果。
如何在表单提交选择字段的选定值后保存并显示?
views.py:
def report(request):
user = request.user
admin = None
try:
admin = user.administrator
except ObjectDoesNotExist:
pass
if not admin:
return render(request, 'vms/no_admin_rights.html')
organization_list = get_organizations_ordered_by_name()
if request.method == 'POST':
form = ReportForm(request.POST)
if form.is_valid():
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
organization = form.cleaned_data['organization']
event_name = form.cleaned_data['event_name']
job_name = form.cleaned_data['job_name']
start_date = form.cleaned_data['start_date']
end_date = form.cleaned_data['end_date']
report_list = get_administrator_report(
first_name,
last_name,
organization,
event_name,
job_name,
start_date,
end_date
)
total_hours = calculate_total_report_hours(report_list)
return render(request, 'administrator/report.html', {'form': form, 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'organization_list': organization_list})
else:
return render(request, 'administrator/report.html', {'form': form, 'notification': False, 'organization_list': organization_list})
else:
form = ReportForm()
return render(request, 'administrator/report.html', {'form': form, 'notification': False, 'organization_list': organization_list})
forms.py:
class ReportForm(forms.Form):
first_name = forms.RegexField(regex=r'^[(A-Z)|(a-z)|(\s)]+$', max_length=30, required=False)
last_name = forms.RegexField(regex=r'^[(A-Z)|(a-z)|(\s)]+$', max_length=30, required=False)
organization = forms.RegexField(regex=r'^[(A-Z)|(a-z)|(\s)]+$', max_length=75, required=False)
event_name = forms.RegexField(regex=r'^[(A-Z)|(a-z)|(0-9)|(\s)|(\.)|(,)|(\-)|(!)]+$', max_length=75, required=False)
job_name = forms.RegexField(regex=r'^[(A-Z)|(a-z)|(\s)]+$', max_length=75, required=False)
start_date = forms.DateField(required=False)
end_date = forms.DateField(required=False)
答案 0 :(得分:1)
更改此行:
<input type="button" id="btnOpenDialog" value="Open Confirm Dialog" />
<div id="dialog-confirm"></div>
<script>
function fnOpenNormalDialog() {
$("#dialog-confirm").html("Confirm Dialog Box");
// Define the Dialog and its properties.
$("#dialog-confirm").dialog({
resizable: false,
modal: true,
title: "Modal",
height: 250,
width: 400,
buttons: {
"Yes": function () {
$(this).dialog('close');
callback(true);
},
"No": function () {
$(this).dialog('close');
callback(false);
}
}
});
}
$('#btnOpenDialog').click(fnOpenNormalDialog);
function callback(value) {
if (value) {
alert("Confirmed");
} else {
alert("Rejected");
}
} </script>
添加到上下文return render(request, 'administrator/report.html', {'form': form, 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'organization_list': organization_list})
:
'selected_organization': organization
报告表格应如下所示:
return render(request, 'administrator/report.html', {'form': form, 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'organization_list': organization_list, 'selected_organization': organization})