Не удалось отправить форму Django с элементом <a> - PullRequest
0 голосов
/ 17 октября 2019

Я пытаюсь заставить Django отправить форму с элементом <a>, но вместо этого используется метод add_employee_action() GET POST

<form id="add-employee-form" action="add/" method="POST">
    <table>
        <tr>
            <th class="table-title" id="second-name">
                Фамилия
            </th>
            <th class="table-title" id="first-name">
                Имя
            </th>
            <th class="table-title" id="patronymic">
                Отчество
            </th>
            <th class="table-title" id="birth-date">
                Дата рождения
            </th>
        </tr>
        <tr>
            <th id="second-name">
                <div class="field-wrapper">
                    {{ form.second_name.errors }}
                    {{ form.second_name }}
                </div>
            </th>
            <th id="first-name">
                <div class="field-wrapper">
                    {{ form.first_name.errors }}
                    {{ form.first_name }}
                </div>
            </th>
            <th id="patronymic">
                <div class="field-wrapper">
                    {{ form.patronymic.errors }}
                    {{ form.patronymic }}
                </div>
            </th>
            <th id="birth-date">
                <div class="field-wrapper" id="birth-date-wrapper">
                    {{ form.birth_date.errors }}
                    {{ form.birth_date }}
                </div>
            </th>
        </tr>
    </table>
    {% csrf_token %}
    <div class="button_bar">
        <a class="a-button positive" href="{% url 'add-employee-action' %}">Добавить</a>
        <a class="a-button" href="{% url 'employee' %}">Сотрудники</a>
        <a class="a-button" href="{% url 'index' %}">На главуную</a>
    </div>
</form>

urls.py

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

views.py

def add_employee_action(request):
    if request.method == "POST":
        form = AddEmployeeForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = AddEmployeeForm()
    context = {
        'form': form,
    }
    return render(request, 'add_employee.html', context)

Как отправить форму с использованием элемента <a>?

1 Ответ

1 голос
/ 17 октября 2019

Просто добавьте прослушиватель событий в тег:

$('#some_tag_id').on('click', function(e) {
    e.preventDefault();
    var form = $('#add-employee-form');
    form.attr("method", "POST");
    form.submit();
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...