Mariana Gomez-Kusnecov
Mariana Gomez-Kusnecov

Reputation: 245

Form not resetting upon submission?

I have a page with a form that once it gets submitted, the form loads again for the next person. I switched from class based views to function based due to a problem I was having to render modals and I noticed that now, since I'm not redirecting to the form again, it does not clear the data that was entered previously. How can I clear the form upon submission?

views.py

def enter_exit_area(request):

    form = WarehouseForm(request.POST or None)
    enter_without_exit = None
    exit_without_enter = None

    if form.is_valid():
        emp_num = form.cleaned_data['employee_number']
        area = form.cleaned_data['work_area']
        station = form.cleaned_data['station_number']

        if 'enter_area' in request.POST:
            # Some rules to open modals/submit

            message = 'You have entered %(area)s' % {'area': area}
            if station is not None:
                message += ': %(station)s' % {'station': station}
            messages.success(request, message)

        elif 'leave_area' in request.POST:
           # more Rules

            message = 'You have exited %(area)s' % {'area': area}
            if station is not None:
                message += ': %(station)s' % {'station': station}
            messages.success(request, message)

    return render(request, "operations/enter_exit_area.html", {
        'form': form,
        'enter_without_exit': enter_without_exit,
        'exit_without_enter': exit_without_enter,
    })

forms.py

class WarehouseForm(AppsModelForm):
    class Meta:
        model = EmployeeWorkAreaLog
        widgets = {
            'employee_number': ForeignKeyRawIdWidget(EmployeeWorkAreaLog._meta.get_field('employee_number').remote_field, site, attrs={'id':'employee_number_field'}),
        }
        fields = ('employee_number', 'work_area', 'station_number', 'edited_timestamp')

enter_exit_area.html

{% extends "base.html" %}
{% load core_tags %}
{% block main %}

    {% if enter_without_exit %}
        <div id="auto-open-ajax-modal" data-modal="#create-update-modal" data-modal-url="{% url "operations:update_timestamp_modal" enter_without_exit.id %}" class="hidden"></div>
    {% endif %}
    {% if exit_without_enter %}
        <div id="auto-open-ajax-modal" data-modal="#create-update-modal" data-modal-url="{% url "operations:update_timestamp_modal" exit_without_enter.id %}" class="hidden"></div>
    {% endif %}

    <form id="warehouseForm" action="" method="POST" class="form-horizontal" novalidate >
        {% csrf_token %}

        <div>
            <div>
                <label>Employee</label>
                {{ form.employee_number }}
            </div>

            <div>
                <label>Work Area</label>
                {{ form.work_area }}
            </div>
                <label>Station</label>
                {{ form.station_number }}
            </div>
        </div>

        <div>
            <div>
                <button type="submit" name="enter_area" value="Enter">Enter Area</button>
                <button type="submit" name="leave_area" value="Leave">Leave Area</button>
            </div>
        </div>
    </form>
    {% modal id="create-update-modal" title="Update Timestamp" primary_btn="Submit" default_submit=True %}
{% endblock main %}

Upvotes: 0

Views: 19

Answers (1)

Mukul Kumar
Mukul Kumar

Reputation: 2103

# Form not resetting upon submission, because you are sending data to your form

form = WarehouseForm(request.POST or None)

# If you want to reset your form, you should not send data to your form

form = WarehouseForm()


For Example:-

if request.method == 'POST':
    form = WarehouseForm(request.POST or None)
else:
    form = WarehouseForm()


# Your views.py can be:-

views.py

def enter_exit_area(request):

    enter_without_exit = None
    exit_without_enter = None

    if request.method == 'POST':
        form = WarehouseForm(request.POST or None)
        if form.is_valid():
            emp_num = form.cleaned_data['employee_number']
            area = form.cleaned_data['work_area']
            station = form.cleaned_data['station_number']

            if 'enter_area' in request.POST:
                # Some rules to open modals/submit

                message = 'You have entered %(area)s' % {'area': area}
                if station is not None:
                    message += ': %(station)s' % {'station': station}
                messages.success(request, message)

            elif 'leave_area' in request.POST:
                # more Rules

                message = 'You have exited %(area)s' % {'area': area}
                if station is not None:
                    message += ': %(station)s' % {'station': station}
                messages.success(request, message)
    else:
        form = WarehouseForm()

    return render(request, "operations/enter_exit_area.html", {
        'form': form,
        'enter_without_exit': enter_without_exit,
        'exit_without_enter': exit_without_enter,
    })

Upvotes: 1

Related Questions