我正在使用modelForm编写一个简单的调查。我也在网上搜索过这个问题并且说它是因为它的形式是未绑定的......但为了简单起见,我只在models.py中提供了一个选择
编辑:form._isbound是真的......所以它是因为别的东西 form.errors显示属性对象0x03A146C0
models.py 那些硬编码为html中的无线电输入
class Office(models.Model):
Office_Space = (
('R1B1', 'R1B1'),
('R2B1', 'R2B1'),
('R3B1', 'R3B1'),
('R1B2', 'R1B2'),
('R2B2', 'R2B2'),
('R3B2', 'R3B2'),
('R1B3', 'R1B3'),
('R2B3', 'R2B3'),
('R3B3', 'R3B3')
)
space = models.CharField(max_length=4, choices=Office_Space)
form.py
class officeForm(forms.ModelForm):
class Meta:
model = Office
fields = ['space',]
Views.py
def get_SenarioChoice(request):
form_class = officeForm(request.POST or None)
if request.method == 'POST':
if form_class.is_valid():
space = request.POST.get('result')
response_data = {}
print(space+ "is valid") # here is the RxCx printed for debugging
response_data['space'] = space
form_class.save()
print (connection.queries) #the SQL log
return JsonResponse(response_data)enter code here
return render(request, 'Front.html', {'officeform': form_class})
补充:模板 - 我对web-dev很新,所以当我写这个表单时,我不知道它可以自己呈现,因此我硬编码了所有内容
调查由3个出价组成,每个出价有3个问题,每个问题有3个选项。 (我可能会将它们分开,但我不知道如何在一个由issID编号的选项中编码它们(" R#")+ BidID(" B#") )
即:R1B1 =第1期出价1
<tr>
<th>Bigger office</th>
</tr>
<tr>
<td>Bigger cubible</td>
<td>5</td>
<td><input type="radio" name="R1B1" value="5" required><br></td>
<td> </td>
<td><input type="radio" name="R1B2" value="5" required><br></td>
<td> </td>
<td><input type="radio" name="R1B3" value="5" required><br></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Shared office</td>
<td>60</td>
<td><input type="radio" name="R1B1" value="60"><br></td>
<td id =R1C1></td>
<td><input type="radio" name="R1B2" value="60"><br></td>
<td id =R1C2></td>
<td><input type="radio" name="R1B3" value="60"><br></td>
<td id = R1C3></td>
<td id =R1C1C></td>
<td id =R1C2C></td>
<td id = R1C3C></td>
</tr>
<tr>
<td>No change</td>
<td>30</td>
<td><input type="radio" name="R1B1" value="30" required><br></td>
<td> </td>
<td><input type="radio" name="R1B2" value="30" required><br></td>
<td> </td>
<td><input type="radio" name="R1B3" value="30" required><br></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<th>New and challenging individual assignments</th>
</tr>
<tr>
<td>Some teamwork, some individual work</td>
<td>80</td>
<td><input type="radio" name="R2B1" value="80" required><br></td>
<td> </td>
<td><input type="radio" name="R2B2" value="80" required><br></td>
<td> </td>
<td><input type="radio" name="R2B3" value="80" required><br></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>No (i.e., no change to current situation)</td>
<td>10</td>
<td><input type="radio" name="R2B1" value="10"><br></td>
<td id =R2C1></td>
<td><input type="radio" name="R2B2" value="10"><br></td>
<td id =R2C2></td>
<td><input type="radio" name="R2B3" value="10"><br></td>
<td id =R2C3></td>
<td id =R2C1C></td>
<td id =R2C2C></td>
<td id =R2C3C></td>
</tr>
<tr>
<td>Mostly Group Work</td>
<td>40</td>
<td><input type="radio" name="R2B1" value="40" required><br></td>
<td> </td>
<td><input type="radio" name="R2B2" value="40" required><br></td>
<td> </td>
<td><input type="radio" name="R2B3" value="40" required><br></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<th>Working hours</th>
</tr>
<tr>
<td>Yes, flextime and others</td>
<td>50</td>
<td><input type="radio" name="R3B1" value="50" required><br></td>
<td> </td>
<td><input type="radio" name="R3B2" value="50" required><br></td>
<td> </td>
<td><input type="radio" name="R3B3" value="50" required><br></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>No change</td>
<td>0</td>
<td><input type="radio" name="R3B1" value="0"><br></td>
<td id =R3C1></td>
<td><input type="radio" name="R3B2" value="0"><br></td>
<td id =R3C2></td>
<td><input type="radio" name="R3B3" value="0"><br></td>
<td id =R3C3></td>
<td id =R3C1C></td>
<td id =R3C2C></td>
<td id =R3C3C></td>
</tr>
<tr>
<tr>
<td>Work more</td>
<td>10</td>
<td><input type="radio" name="R3B1" value="10" required><br></td>
<td> </td>
<td><input type="radio" name="R3B2" value="10" required><br></td>
<td> </td>
<td><input type="radio" name="R3B3" value="10" required><br></td>
<td> </td>
提前致谢。