【问题标题】:How to render template after failed form validation?表单验证失败后如何渲染模板?
【发布时间】:2020-02-23 01:56:55
【问题描述】:

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'),
]

我有add-employee 页面和一些表格要在那里填写。

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,
        }
    )

模板:

<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>

按元素提交已经过测试,并且与 jQuery 脚本配合良好。

问题是在提交无效表单后,我有一个带有blah-blah/employee/add_employee/add/ URL 的页面。如果我再次尝试提交表单,我有一个带有blah-blah/employee/add_employee/add/add/ URL 的页面,这是不正确的。如何使用blah-blah/employee/add_employee/ URL 呈现页面并显示所有错误消息?

【问题讨论】:

  • 这可能是因为在&lt;form&gt;action=... 中使用了relative url。
  • @WillemVanOnsem,是的,它有action="add/"。但是我该如何解决呢?
  • 你能把你的模板也展示一下吗

标签: django django-forms django-urls


【解决方案1】:

这可能是因为您在sections/add_employee.html 模板的&lt;form&gt; 标记中编写了一个相对 URL。因此,模板包含以下内容:

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

您可以使用带有{% url … %} template tag [Django-doc] 的网址:

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

此外,通常使用 same 路径来处理 GET 和 POST 请求。所以实际上你可以简单地删除'add-employee' 路径。

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多