我有一个模型和一个modelform来更改一些设置。表单显示正常,值正确,但是当我提交表单时,请求中缺少一个字段。帖子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")
view类(我使用的是Django 1.3中基于类的视图):
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>
下面是运行调试服务器时控制台的调试输出:
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']}>",)
可以看到,在request.POST的QueryDict中没有字段tick_time。
应该注意的是,该字段是在web浏览器中,当查看HTML源代码时,它看起来就像表单中的其他字段。
谁有任何提示什么可能是错误的?
既然你使用的是通用视图,是不是最好扩展ProcessFormView而不是TemplateView
?
编辑
我用TemplateView
试过你的代码:
class EditNodeView(TemplateView):
你有一个get_context_data
来推动表单吗?
def get_context_data(self, **kwargs):
instance = NodeSettings.objects.get(pk=kwargs['node_id'])
form = EditNodeView.NodeSettingsForm(instance=instance)
context = super(EditNodeView, self).get_context_data(**kwargs)
context['form'] = form
return context
编辑现有对象的最佳方法是通过主键获取,我在urls.py
中有以下内容:
url(r'^edit-node/(?P<node_id>d+)/$', EditNodeView.as_view(), name='edit-node'),
我通过主键获取实例,可能需要做一些上面的检查,如抛出404,如果不存在。
在你的模型中,你有nodetype
和nodeserial
作为editable=False
,如果它们被设置为不可编辑,你如何显示或创建这些项目?为了测试,我将它们设置为True
。
<form action="" method="POST">
我知道有很多变化,但上面可以正确地查看和编辑你的模型。您可以在表单级别将nodetype
和nodeserial
设置为只读,以防止人们编辑它们。