如何为post方法命名众多动态输入字段-Django [英] how to name numerous dynamic input fields for post method - Django
问题描述
在我的网上商店中,我从两个不同的应用程序中获取了所有产品和服务,并将其列出以供用户制作愿望清单.
In my online shop, I fetch all of the products and services from two different apps and list them for the user to make his wishlist.
每种产品或服务都显示在引导卡中,该引导卡包含用于产品计数的输入字段.
Each product or service is displayed in a bootstrap card that contains an input field for the count of products.
#views.py
def my_products(request):
ip_sensor = Ip_sensor.objects.all().order_by('title')
control_valves = ControlValves.objects.all().order_by('title')
context = {
'ip_sensor': ip_sensor,
'control_valves': control_valves,
}
return render(request, 'users/basket/my_products.html', context)
然后在模板中,它们以这种方式显示:
then in the template, they are being displayed in this way:
<form method="post" id="add_to_wishlist" data-url="{% url 'my_products' %}">
{% csrf_token %}
{% if ip_sensor %}
{% for item in ip_sensor %}
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-12">
<p class="card-text text-center text-capitalize">{{ item.title }}</p>
</div>
<div class="col-12">
<div class="form-group">
<input type="hidden" name="tag" value="{{ item.type }}"> <!-- what to put in name field of this line -->
<input type="hidden" name="item_id" value="{{ item.id }}"> <!-- what to put in name field of this line -->
<label for="count" class="control-label">count</label>
<input type="text"
id="count"
name="count" <!-- what to put in name field of this line -->
placeholder="Count"
class="form-control"
autofocus/>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
{% endif %}
</form>
问题:
当我通过POST方法将每个产品或服务的计数返回到我的 views.py
并保存到他的愿望清单时,我不知道如何区分返回的产品或服务值?
When I return the count of each product or service through a POST method back to my views.py
to save to his wishlist, I don't know how to distinguish between the returned values??
由于项目在for循环中显示,并且我想将每个选择的项目分别保存在 WishListItem
对象(模型)中,因此我需要分别命名每个卡的输入字段,但是我不知道不知道该怎么做.
Since items are being displayed in a for loop and I want to save each of the selected items separately in a WishListItem
object (model), I need to name each card's input fields separately but I don't know how to do it.
我可以通过以下方式保存每个项目:
I can save each item in this way:
if request.method == 'POST':
owner = request.user
count = request.POST.get('count')
tag = request.POST.get('tag')
object_id = request.POST.get('item_id')
wishlist = WishListItem(owner=owner,
content_type=class_types[tag],
object_id=object_id,
tag=tag,
count=count)
wishlist.save()
return redirect('my_products')
推荐答案
当有多个同名输入时, request.POST
会列出所有这些输入元素值.因此,您可以使用 request.POST.getlist('item_id')
获取所有商品ID,它将返回包含所有ID的列表.在您的html中,您可以使用 id
作为 name
属性的一部分来命名所有其他输入,如下所示:
When there are multiple inputs with same name, request.POST
has list of all those input element values. So, you can get all item ids using request.POST.getlist('item_id')
and it will return list containing all ids. In your html you can name all other inputs using id
as part of the name
attribute, like this:
...
<input type="hidden" name="tag_{{ item.id }}" value="{{ item.type }}"> <!-- item id has become part of the input name -->
<input type="hidden" name="item_id" value="{{ item.id }}"> <!-- all item ids will be accessed as list in view -->
<label for="count" class="control-label">count</label>
<input type="text"
id="count"
name="count_{{ item.id }}" <!-- item id has become part of the input name -->
placeholder="Count"
class="form-control"
autofocus/>
...
在您的视图中,您可以访问以下所有值:
And in your view you can access all values like this:
...
for object_id in request.POST.getlist('item_id'): #this will contain a list with all item ids in it
count = request.POST.get('count_%s'%object_id) #as html inputs are named with ids as part of name you can access them
tag = request.POST.get('tag_%s'%object_id) #as html inputs are named with ids as part of name you can access them
wishlist = WishListItem(owner=owner,
content_type=class_types[tag],
object_id=object_id,
tag=tag,
count=count)
wishlist.save()
...
这篇关于如何为post方法命名众多动态输入字段-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!