I have a model and a modelform to change some settings. The form is displayed allright, with the correct values, but when I submit the form one field is missing in the request.POST dict.
模式:
class NodeSettings(models.Model):
nodetype = models.CharField(max_length=8, editable=False)
nodeserial = models.IntegerField(editable=False)
upper_limit = models.FloatField(null=True, blank=True,
help_text="Values above this limit will be of different color.")
graph_time = models.IntegerField(null=True, blank=True,
help_text="The `width of the graph, in minutes.")
tick_time = models.IntegerField(null=True, blank=True,
help_text="Number of minutes between `ticks in the graph.")
graph_height = models.IntegerField(null=True, blank=True,
help_text="The top value of the graphs Y-axis.")
class Meta:
unique_together = ("nodetype", "nodeserial")
观点类别(采用Django1.3的、有等级观点的Im):
class EditNodeView(TemplateView):
template_name = live/editnode.html
class NodeSettingsForm(forms.ModelForm):
class Meta:
model = NodeSettings
# Some stuff cut out
def post(self, request, *args, **kwargs):
nodetype = request.POST[ nodetype ]
nodeserial = request.POST[ nodeserial ]
# logger is a Django logger instance defined in the settings
logger.debug( nodetype = %r % nodetype)
logger.debug( nodeserial = %r % nodeserial)
try:
instance = NodeSettings.objects.get(nodetype=nodetype, nodeserial=nodeserial)
logger.debug( have existing instance )
except NodeSettings.DoesNotExist:
instance = NodeSettings(nodetype=nodetype, nodeserial=nodeserial)
logger.debug( creating new instance )
logger.debug( instance.tick_time = %r % instance.tick_time)
try:
logger.debug( POST[tick_time] = %r % request.POST[ tick_time ])
except Exception, e:
logger.debug( error: %r % e)
form = EditNodeView.NodeSettingsForm(request.POST, instance=instance)
if form.is_valid():
from django.http import HttpResponse
form.save()
return HttpResponse()
else:
return super(EditNodeView, self).get(request, *args, **kwargs)
模板的相关部分:
<form action="{{ url }}edit_node/" method="POST">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Ok" />
</form>
The debugproducts:
2011-04-12 16:18:05,972 DEBUG nodetype = u V10
2011-04-12 16:18:05,972 DEBUG nodeserial = u 4711
2011-04-12 16:18:06,038 DEBUG have existing instance
2011-04-12 16:18:06,038 DEBUG instance.tick_time = 5
2011-04-12 16:18:06,039 DEBUG error: MultiValueDictKeyError("Key tick_time not found in <QueryDict: {u nodetype : [u V10 ], u graph_time : [u 5 ], u upper_limit : [u ], u nodeserial : [u 4711 ], u csrfmiddlewaretoken : [u fb11c9660ed5f51bcf0fa39f71e01c92 ], u graph_height : [u 25 ]}>",)
如你所看到,实地标准——时间在请求书的空白处没有。 POST。
应当指出,这个领域属于网络浏览器,在研究超文本来源时,它仅以形式看待其他领域。
任何人在什么情况下会错?