django post返回令牌不发布数据

时间:2014-06-20 17:38:59

标签: django django-models django-forms django-templates django-views

我正在从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门户网站(请求):

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']

喜欢这样吗?

2 个答案:

答案 0 :(得分:1)

您需要将name属性放在select元素上,而不是option

但是你应该使用Django的表单框架。

答案 1 :(得分:0)

正如@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>
相关问题