const url = 'http://example.com/';
final response = await http.get(url);
final decodedResponse = json.decode(response);
final yourObject = Class.fromJSON(decodedResponse);
:
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,
}
)
按元素提交已经过测试,并且可以与jQuery脚本一起很好地工作。
问题在于,提交无效的表单后,我的页面带有<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>
URL。而且,如果我尝试再次提交表单,则我的页面带有blah-blah/employee/add_employee/add/
URL,这是不正确的。如何使用blah-blah/employee/add_employee/add/add/
URL呈现页面并显示所有错误消息?
答案 0 :(得分:2)
这可能是因为您在<form>
模板的sections/add_employee.html
标记中写了 relative URL。因此,模板包含以下内容:
<form method="post" action="add/">
...
</form>
您可以将网址与https://www.vagrantup.com/一起使用:
<form method="post" action="{% url 'add-employee-action' %}">
...
</form>
此外,通常使用 same 路径来处理GET和POST请求。因此,实际上您可能只是删除了'add-employee'
路径。