简体   繁体   English

Django表单:提交表单后选择字段

[英]Django form: select field after the submit of the form

I have a report form report.html: 我有一个报告表格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>

After the submitting this form the selected organization is not saved. 提交此表格后,所选的组织不会保存。 But it would be better if the organization will be displayed. 但是,最好是显示组织。 It can be helpful for the user: she/he can see, what filter displays these results. 这可能对用户有帮助:他/他可以看到,哪些过滤器显示了这些结果。

How to save and show after form submitting the selected value for the select field? 表单提交选择字段的选定值后如何保存并显示?

views.py: 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: 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)

Change this line: 更改此行:

return render(request, 'administrator/report.html', {'form': form, 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'organization_list': organization_list})

add to the context 'selected_organization': organization : 添加到上下文'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})

And report form should look like this: 报告表单应如下所示:

<select id="select" class="form-control" name="organization">
    <option value="">-- Select Organization --</option>
    {% for organization in organization_list %}
    <option value="{{ organization.name }}" {% if selected_organization == organization %}selected{% endif %}>{{ organization.name }}</option>
    {% endfor %}
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM