Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 - Fatal编程技术网

Python 保存时缺少Django表单字段

Python 保存时缺少Django表单字段,python,django,Python,Django,我有一个面向用户的表单,它是从模型生成的。该模型有一个指向站点“Section”的外键。当我提交表单时,它在Section\u id列中返回一个IntegrityError null值,该值违反了not null约束。我认为这个问题很简单,直到我开始研究它。该错误仅发生在我的开发服务器CentOS 5.5最终版上。我在本地环境Mac OS X 10.5.8上没有此问题 我进入python shell并检查表单对象;我注意到我的开发环境中缺少“section”字段form.fields.secti

我有一个面向用户的表单,它是从模型生成的。该模型有一个指向站点“Section”的外键。当我提交表单时,它在Section\u id列中返回一个IntegrityError null值,该值违反了not null约束。我认为这个问题很简单,直到我开始研究它。该错误仅发生在我的开发服务器CentOS 5.5最终版上。我在本地环境Mac OS X 10.5.8上没有此问题

我进入python shell并检查表单对象;我注意到我的开发环境中缺少“section”字段form.fields.section。项目代码和django版本在我的本地版和开发版上都是相同的。更让我沮丧的是,面向用户的表单确实有一个隐藏的“section”字段,但当该数据发布到视图时,表单字段不再存在。如果您能深入了解这个错误的原因,我将不胜感激,并为我省下罗根的费用

提前感谢,, 克里斯

代码:

views.py

def event_registration(request, slug):
    section = get_object_or_404(Section, section__slug=slug)
    parent_page = Page.objects.get(section=section, slug='overview')
    page = Page.objects.get(section=section, slug='events-registration')

    form = EventRegistrationForm(label_suffix='')
    submitted = False

    if request.POST.has_key('event'):
        form = EventRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            submitted = True

    context = RequestContext(request)
    return render_to_response('section/event_registration.html',
                            {
                                'section': section,
                                'page': page,
                                'form': form,
                                'submitted': submitted
                            },
                            context_instance=context)
forms.py

class EventRegistrationForm(better_forms.BetterModelForm):

    class Meta:
        model = EventRegistration
        fieldsets = [
                        ('Step 1', {
                            'fields': ['event', 'guests']
                        }),
                        ('Step 2', {
                            'fields': ['first_name', 'email', 'last_name', 'phone', 'school_name']
                        }),
                    ]
models.py

class EventRegistration(models.Model):
    section = models.ForeignKey(Theater, verbose_name=_('Section'), 
        help_text=_("The Site Section"))
    event = models.ForeignKey(Event, verbose_name=_('Event'))
    guests = models.IntegerField(_('Number of Guests?'))
    first_name = models.CharField(_('First Name'), max_length=100)
    last_name = models.CharField(_('Last Name'), max_length=100)
    email = models.CharField(_('E-Mail Address'), max_length=255)
    phone = PhoneNumberField(_('Daytime Phone Number'))
    school_name = models.CharField(_('School Name'), max_length=200, blank=True, null=True)
克里斯


这听起来像是数据库版本不同的问题。您是在不同的时间同步数据库,还是在开发服务器和本地计算机上使用不同的数据库?您还可以发布完整的模型和表单作为示例吗?

请尝试以下内容:

def event_registration(request, slug):
    section = get_object_or_404(Section, section__slug=slug)
    parent_page = Page.objects.get(section=section, slug='overview')
    page = Page.objects.get(section=section, slug='events-registration')

    eventr = EventRegistration(section=section)
    submitted = False

    if request.method == 'POST':
        form = EventRegistrationForm(request.POST, instance=eventr)
        if form.is_valid():
            form.save()
            submitted = True
    else:
        form = EventRegistrationForm(label_suffix='', instance=eventr)

    context = {
                 'section': section,
                 'page': page,
                 'form': form,
                 'submitted': submitted
              }
    return render_to_response('section/event_registration.html', context,
                            context_instance=RequestContext(request))
编辑:

从技术上讲,如果你刚刚改变了

form.save()
to
instance = form.save(commit=False)
instance.section=section
instance.save()

这也会起作用。在将新EventRegistration提交到db之前,您需要填写它的section字段。

发布一些代码。i、 e.forms.py、views.py、models.py您可以发布整个视图吗?您没有为表单设置实例,因此在尝试保存时没有节值。“如果你发表更多的观点,我可能会给你一个答案。好的,我有完整的观点。”肯齐克发表了一个答案,但去了一个实验室会议。我会在几个小时后回来,详细说明或澄清我的答案。他的数据库似乎没有同步。他收到了一个数据库完整性错误,因为他的窗体没有将值传递给他的fk节,所以它试图将null保存到受not null约束的db列。数据库似乎已同步。它成功了!非常感谢。你知道为什么我的代码在我的本地服务器上工作吗?这是一个猜测。。。但是您的本地数据库可能没有节NOTNULL约束