【发布时间】:2020-02-10 22:00:32
【问题描述】:
我的应用中有两个视图
Views.py
def selectwarehouse(request):
z = Warehouse.objects.all()
return render(request, 'packsapp/employee/selectwarehouse.html', {'z': z})
def warehouse_details(request):
queryset = AllotmentDocket.objects.filter(send_from_warehouse = #dynamic(from the selectwarehouse.html))
return render(request, 'packsapp/employee/allotwarehousedetails.html', {'query': queryset})
selectwarehouse.html
{% block content %}
<label>Select Warehouse<label>
<select id="the-id">
{% for i in z %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
<form method="post" novalidate>
{% csrf_token %}
<a href="{% url 'employee:warehouse_details' %}" class="btn btn-outline-secondary" role="button">Proceed</a>
<a href="{% url 'employee:products_table' %}" class="btn btn-outline-secondary" role="button">Nevermind</a>
</form>
</select>
{% endblock %}
URLS.py
path('select-warehouse/', selectwarehouse, name='select_warehouse'),
path('warehouse-details/', warehouse_details, name='warehouse_details'),
我希望当一个人从下拉列表中选择“仓库”并单击 Proceed 时,它应该将该值传递给 def warehouse_details 并将该值传递给分配查询集。我该怎么做?
【问题讨论】:
-
您需要在
<form ...>中指定action="url/to/warehouse_details/view"。你的<option>s 应该是表单的一部分 -
@YevhenKuzmovych 而不是href,我应该将它定向到warehouse_details 视图?
-
是的,我想是的。否则,该按钮只会将您重定向到带有
GET而不是POST的employee:warehouse_detailsURL,并且没有来自<option>s 的任何信息。 -
@YevhenKuzmovych 我是 django 模板的新手,你能告诉我怎么做吗?
-
您的 html 表单由内而外,
<a/>元素在 html 表单处理中没有任何作用。了解 HTML 以使用 django 很重要。阅读一些教程。 developer.mozilla.org/en-US/docs/Learn/HTML/Forms/…