- 首页
- 前端开发
- 如何为 post 方法命名大量动态输入字段 - Django
如何为 post 方法命名大量动态输入字段 - Django
[英] how to name numerous dynamic input fields for post method - Django
本文介绍了如何为 post 方法命名大量动态输入字段 - Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的在线商店中,我从两个不同的应用程序中获取所有产品和服务,并将它们列出以供用户制作他的愿望清单.
每个产品或服务都显示在一个引导卡片中,其中包含一个用于产品数量的输入字段.
#views.pydef my_products(请求):ip_sensor = ip_sensor.objects.all().order_by('title')control_valves = ControlValves.objects.all().order_by('title')上下文 = {'ip_sensor': ip_sensor,'控制阀':控制阀,}返回渲染(请求,'users/basket/my_products.html',上下文)
然后在模板中,它们以这种方式显示:
{% 结束为 %}{% 万一 %}</表单>
问题:
当我通过POST方法将每个产品或服务的数量返回到我的views.py
保存到他的wishlist时,我不知道如何区分返回的价值观?
由于项目显示在 for 循环中,我想将每个选定的项目分别保存在 WishListItem
对象(模型)中,我需要分别命名每张卡片的输入字段,但我不不知道该怎么做.
我可以这样保存每个项目:
if request.method == 'POST':所有者 = request.usercount = request.POST.get('count')tag = request.POST.get('tag')object_id = request.POST.get('item_id')Wishlist = WishListItem(owner=owner,content_type=class_types[标签],object_id=object_id,标签=标签,计数=计数)心愿单.save()返回重定向('my_products')
解决方案
当有多个同名输入时,request.POST
会列出所有这些输入元素的值.因此,您可以使用 request.POST.getlist('item_id')
获取所有项目 ID,它将返回包含所有 ID 的列表.在您的 html 中,您可以使用 id
作为 name
属性的一部分来命名所有其他输入,如下所示:
...<输入类型=隐藏"name="tag_{{ item.id }}";值={{ item.type }}"><!-- item id 已成为输入名称的一部分--><输入类型=隐藏"名称=item_id"值={{ item.id }}"><!-- 所有项目 id 都将作为视图中的列表访问 --><label for="count";class="control-label">count<输入类型=文本"id=计数"name="count_{{ item.id }}";<!-- item id 已成为输入名称的一部分-->占位符=计数"类=表单控制"自动对焦/>...
在您看来,您可以像这样访问所有值:
...for object_id in request.POST.getlist('item_id'): #this 将包含一个包含所有项目 ID 的列表count = request.POST.get('count_%s'%object_id) #因为html输入以ids作为名称的一部分命名,你可以访问它们tag = request.POST.get('tag_%s'%object_id) #因为html输入以ids作为名称的一部分命名,你可以访问它们Wishlist = WishListItem(owner=owner,content_type=class_types[标签],object_id=object_id,标签=标签,计数=计数)心愿单.save()...
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>
Question:
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??
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')
解决方案
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屋!