Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django表单缺少字段_Python_Django_Django Models_Django Forms - Fatal编程技术网

Python Django表单缺少字段

Python Django表单缺少字段,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我有一个模型和一个模型表单来更改一些设置。表单显示正确,值正确,但当我提交表单时,request.POST dict中缺少一个字段 模型: class NodeSettings(models.Model): nodetype = models.CharField(max_length=8, editable=False) nodeserial = models.IntegerField(editable=False) upper_limit = models.FloatF

我有一个模型和一个模型表单来更改一些设置。表单显示正确,值正确,但当我提交表单时,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")
视图类(我正在使用Django 1.3和基于类的视图):

模板的相关部分:

<form action="{{ url }}edit_node/" method="POST">
  {% csrf_token %}
  <table>
    {{ form.as_table }}
  </table>
  <input type="submit" value="Ok" />
</form>

{%csrf_令牌%}
{{form.as_table}}
以下是运行调试服务器时控制台中的调试输出:

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']}>",)
2011-04-12 16:18:05972调试节点类型=u'V10'
2011-04-12 16:18:05972调试节点序列=u'4711'
2011-04-12 16:18:06038调试现有实例
2011-04-12 16:18:06038调试实例。勾选时间=5
2011-04-12 16:18:06039调试错误:多值DICTKEYERROR(“在中找不到键'tick_time'”,)
如您所见,QueryDict from request.POST中没有“勾选时间”字段

应该注意的是,该字段位于web浏览器中,当查看HTML源代码时,它看起来与表单中的其他字段一样


有人对可能出现的错误有任何提示吗?

既然您使用的是泛型视图,那么扩展而不是
模板视图是不是最好

编辑

我已使用
模板视图
尝试了您的代码:

class EditNodeView(TemplateView):
您是否有
get\u context\u 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
编辑现有对象的最佳方法是通过主键获取,我在
url.py
中有以下内容:

url(r'^edit-node/(?P<node_id>\d+)/$', EditNodeView.as_view(), name='edit-node'),

我知道有很多更改,但是上面的内容可以正确地查看和编辑您的模型。您可以在表单级别将
节点类型
节点序列
设置为只读,以防止人们对其进行编辑。

转换为使用FormView而不是TemplateView,但基本问题仍然是request.POST缺少“tick_time”字段。优先级已更改,因此这将暂时搁置,但我会在一两周内仔细看看你的答案。
url(r'^edit-node/(?P<node_id>\d+)/$', EditNodeView.as_view(), name='edit-node'),
<form action="" method="POST">