[Fixed]-Django form: select field after the submit of the form

1👍

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:

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>

Leave a comment