Search code examples
djangodjango-formsdjango-urls

How to render template after failed form validation?


urls.py:

urlpatterns = [
    path('employee/add_employee/', views.add_employee, name='add-employee'),
    path('employee/add_employee/add/', views.add_employee_action, name='add-employee-action'),
]

I have add-employee page and some forms to fill there.

views.py:

def add_employee(request):
    personal_form = PersonalEmployeeForm()
    history_form = EmployeeHistoryForm()
    return render(
        request,
        'sections/add_employee.html',
        context={
            'personal_form': personal_form,
            'history_form': history_form,
            }
    )

def add_employee_action(request):
    if request.method == "POST":
        personal_form = PersonalEmployeeForm(request.POST)
        history_form = EmployeeHistoryForm(request.POST)
        if personal_form.is_valid() and history_form.is_valid():
            # here is some logic with models
            return redirect('add-employee')
    else:
        personal_form = PersonalEmployeeForm()
        history_form = EmployeeHistoryForm()
    return render(
        request,
        'sections/add_employee.html',
        context={
            'personal_form': personal_form,
            'history_form': history_form,
        }
    )

template:

<form id="a-submit-form" action="add/" method="POST">
        {% csrf_token %}
        <div class="column-wrapper">
            <div class="column">
                <div class="form-wrapper">
                    {% for field in personal_form.visible_fields %}
                        {% include "elements/forms/form_line.html" %}
                        <br>
                    {% endfor %}
                </div>
            </div>
            <div class="column">
                <div class="form-wrapper">
                    {% for field in history_form.visible_fields %}
                        {% include "elements/forms/form_line.html" %}
                        <br>
                    {% endfor %}
                </div>
            </div>
        </div>
        <div class="button-bar-wrapper">
            <div class="button_bar">
                <a class="a-button positive" id="submit">Добавить</a>
                <a class="a-button" href="{% url 'employee' %}">Сотрудники</a>
                <a class="a-button" href="{% url 'index' %}">На главуную</a>
            </div>
        </div>
    </form>

Submitting by <a> element is tested and worked well with jQuery script.

The problem is after submitting invalid forms I have a page with blah-blah/employee/add_employee/add/ URL. And if I try to submit forms again I have a page with blah-blah/employee/add_employee/add/add/ URL, which is incorrect. How can I render the page with blah-blah/employee/add_employee/ URL and show all error messages?


Solution

  • This is likely because you have written a relative URL in the <form> tag of the sections/add_employee.html template. The template thus contains something like:

    <form method="post" action="add/">
        ...
    </form>

    You can use a URL with the {% url … %} template tag [Django-doc]:

    <form method="post" action="{% url 'add-employee-action' %}">
        ...
    </form>

    Furthermore one usually uses the same path to handle both the GET and the POST request. So in fact you might simply remove the 'add-employee' path.