[英]Django template pass array to input field
I have a hidden input field in my django template where I'm passing as value an array. 我的django模板中有一个隐藏的输入字段,在这里我将值作为数组传递。
<form method="post" action="{% url 'flights:flight-selection' %}">{% csrf_token %}
<input type="hidden" id="fa_f_ids" name="fa_f_ids" value="{{ value.fa_f_ids }}">
<button type="submit" class="btn btn-light">Select</button>
</form>
When I submit this form via post request I want to get the value of fa_f_ids
as an array but I am getting a string instead when I'd like to get an array. 当我通过邮寄请求提交此表单时,我想获取
fa_f_ids
的值作为数组,但是当我想要获取数组时,我得到的是字符串。
request.POST.get("fa_flight_id")
#=> <QueryDict: {'csrfmiddlewaretoken': ['UoYqbTUlNxTEJW5AUEfgsgsLuG63dUsvX88DkwGLUJfbnwJdvcfsFhi75yie5uMX'], 'fa_f_ids': ["['AMX401-1560750900-schedule-0000', 'AMX19-1560782100-schedule-0001']"]}>
You need to split the array into several hidden fields, each representing one position of your array: 您需要将数组拆分为几个隐藏的字段,每个字段代表数组的一个位置:
<form method="post" action="{% url 'flights:flight-selection' %}">
{% csrf_token %}
{% for val in value.fa_f_ids %}
<input type="hidden" name="fa_f_ids[{{ forloop.counter0 }}]" value="{{ val }}">
{% endfor %}
<button type="submit" class="btn btn-light">Select</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.