django发布回复令牌不发布数据
我正在从Django DB内容动态生成的网页发布帖子。 数据放在组合框中,应该将客户ID发回给Django,以便我可以解析更多查询,然后仅显示选定的客户数据。
然而,不要重新发布我的发布数据,它会返回csrf标记而不是发布的数据。
<form method='post' action='' id='customers'>
{% csrf_token %}
        <select>
        {% for item in customer %}
        <option value={{ item.customer_id }} name='customer_id'>{{ item.customer_name }}</option>
        {% endfor %}
        <input type = 'submit' value='Edit'>
        </select>
{{ post }}
{{post.customer_id}} #doesnt work either.
这就是它返回的结果:
<QueryDict: {u'csrfmiddlewaretoken': [u'CpzKrwmZsmfiiNHngNWDFSNxqUoBykYO']}>
def portal(请求):
customers = Customer.objects.all()
if request.method == 'POST':
   vm_groups = Vm_group.objects.all()
   vms = Vm.objects.all()
   selected_customer = request.POST.copy()
   #selected_customer_id = selected_customer['customer_id']
   post = selected_customer
   context = Context({'customer': customers, 'vm_groups':vm_groups, 'vms':vms, 'post':post, 
                                               })
   return render(request, 'portal.html', context)
else:
   context = Context({'customer': customers,})
   return render(request, 'portal.html', context)
一旦它正常工作,我怎样从返回的数据中得到customer_id?
selected_customer = request.POST.copy()
selected_customer_id = selected_customer['customer_id']
像这样?
  您需要将name属性放在select元素上,而不是option 。 
但是,无论如何,你可能应该使用Django的表单框架。
正如@Daniel所说的那样,您需要添加名称字段来选择标签而不是选项标签,
<select name='customer_id'>
        {% for item in customer %}
        <option value={{ item.customer_id }} >{{ item.customer_name }}</option>
        {% endfor %}
        <input type = 'submit' value='Edit'>
        </select>
上一篇: django post returning token not post data
下一篇: How to send request from one django server to another server
