我的应用中有两个 View
View .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 %}
网址.py
path('select-warehouse/', selectwarehouse, name='select_warehouse'),
path('warehouse-details/', warehouse_details, name='warehouse_details'),
我希望当一个人从下拉列表中选择“仓库”并点击继续时,它应该将该值传递给def warehouse_details
并将该值传递给Allotment查询集。我该怎么做?
最佳答案
MAKE 作为隐藏输入
模板
{% block content %}
<label>Select Warehouse<label>
<select id="the-id">
{% for i in z %}
<option value="{{ i }}">{{ i }}</option>
<form method="post" novalidate>
{% csrf_token %}
<input type="hidden" name="object_id" value="{{ i.id }}">
<input class="btn btn-outline-secondary" name="Proceed" type="submit"
value="Proceed">
<a href="{% url 'employee:products_table' %}" class="btn btn-outline-
secondary" role="button">Nevermind</a>
</form>
{% endfor %}
</select>
{% endblock %}
View .py
def selectwarehouse(request):
z = Warehouse.objects.all()
return render(request, 'packsapp/employee/selectwarehouse.html', {'z': z})
def warehouse_details(request):
queryset = AllotmentDocket.objects.get(id=request.POST.get('object_id'))
//Now to access the element in queryset write (queryset.'attribute name')
return render(request, 'packsapp/employee/allotwarehousedetails.html', {'query':
queryset}
检查这是否有效
尝试打印查询集并查看输出结果。
关于html - 将数据从一个 View 传递到另一个 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58374044/