我在一个页面上有多个表单,我的视图是通过检查提交的值来处理这些表单。这似乎工作得很好,但是在我的表格中得到以下错误。
'QueryDict' object has no attribute 'method'
查看
def all(request):
if request.method == 'POST':
if 'all' in request.POST['submit']:
all(request.POST)
elif 'addtype' in request.POST['submit']:
addtype(request.POST)
elif 'addnewpm' in request.POST['submit']:
addnewpm(request.POST)
elif 'addnewspec' in request.POST['submit']:
addnewspec(request.POST)
elif 'update' in request.POST['submit']:
update(request.POST)
elif 'addnewrecord' in request.POST['submit']:
addnewrecord(request.POST)
基本上我只是根据按下的提交按钮将post值传递给单独的函数。除了第一个'all'之外,它们都能正常工作。 'all'提交按钮提交了大量数据,我可以在追溯中看到所有这些数据。
可能与我的HTML代码有关。
<table class="gridtable">
<tr>
<td class="topheader-left" colspan="10">
<form action="" method="post">
<button type="submit" value="all" name="submit" style="border:0px;">
<img src="{% get_static_prefix %}images/update.png" style="width:27px;height:27px;">
</button>
</td>
</tr>
在此之下,我只有大量的表格单元格,其中包含字段和/表格。
我页面上其中一个表单的代码运行正常。
<table width="100%">
<tr>
<form method="post" action="">
<td>
<input id="newtype" type="text" name='newtype' size="40" value="Service Type">
</td>
<td>
<button name="submit" type="submit" value="addtype" style="border:0px;">
<img src="{% get_static_prefix %}images/Add-icon.png" width="20" height="20" border="0">
</button>
</td>
</form>
此表单似乎工作正常。我不明白我在做什么不同。
干杯。
答案 0 :(得分:1)
好像是一个简单的函数名碰撞。您的查看方法名称为all
,您一次又一次地调用all(request)
,如果submit == all
的值,则再次调用{。}}。
使用in
在request.POST中查找提交的值似乎很奇怪。为什么不设置一次值并以这种方式进行比较呢?
submit = request.POST['submit']
if submit == 'all':
# call method
elif submit == 'addtype':
# etc