Python Django 1.10模型形式保存问题
我正在与一些应该简单的事情作斗争。事实上,我以前也做过这件事,我似乎无法理解正在发生的事情 我只想从前端保存一个模型表单 我在Django 1.10频道 models.pyPython Django 1.10模型形式保存问题,python,django,modelform,Python,Django,Modelform,我正在与一些应该简单的事情作斗争。事实上,我以前也做过这件事,我似乎无法理解正在发生的事情 我只想从前端保存一个模型表单 我在Django 1.10频道 models.py class Information(forms.ModelForm): class Meta: model = Fathers fields = ('id','first','middle','last','city_of_birth','region_of_birth','count
class Information(forms.ModelForm):
class Meta:
model = Fathers
fields = ('id','first','middle','last','city_of_birth','region_of_birth','country_of_birth',)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class='form-horizontal'
self.helper.form_id = 'id-information'
self.helper.form_method = 'post'
self.helper.layout = Layout(
Div(
FormActions(Submit('dbPostother', 'Save data', css_class='btn btn-success btn-lg', action=".")),
css_class = 'col-md-3'
)
)
super(Information, self).__init__(*args,**kwargs)
class Fathers(models.Model):
id = models.BigIntegerField(primary_key = True,default=1)
last = models.TextField(blank=True, null=True)
first = models.TextField(blank=True, null=True)
middle = models.TextField(blank=True, null=True)
city_of_birth = models.ForeignKey(CitiesCity,blank=True, null=True)
country_of_birth = models.ForeignKey(CitiesCountry,blank=True, null=True)
region_of_birth = models.TextField(blank=True,null=True)
def __unicode__(self):
return self.first +" "+ self.last
class Meta:
managed = True
db_table = 'fathers'
verbose_name = 'Father'
verbose_name_plural = 'Fathers'
url.py
url(r'^dbPostother$',app.views.dbPostother,name='dbPostother'),
views.py
def dbPostother(request):
if request.method == 'POST':
form = Information(request.POST)
if form.is_valid():
form.save()
context = {'year':datetime.now().year}
return render(request, 'app/index.html', context)
models.py
class Information(forms.ModelForm):
class Meta:
model = Fathers
fields = ('id','first','middle','last','city_of_birth','region_of_birth','country_of_birth',)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class='form-horizontal'
self.helper.form_id = 'id-information'
self.helper.form_method = 'post'
self.helper.layout = Layout(
Div(
FormActions(Submit('dbPostother', 'Save data', css_class='btn btn-success btn-lg', action=".")),
css_class = 'col-md-3'
)
)
super(Information, self).__init__(*args,**kwargs)
class Fathers(models.Model):
id = models.BigIntegerField(primary_key = True,default=1)
last = models.TextField(blank=True, null=True)
first = models.TextField(blank=True, null=True)
middle = models.TextField(blank=True, null=True)
city_of_birth = models.ForeignKey(CitiesCity,blank=True, null=True)
country_of_birth = models.ForeignKey(CitiesCountry,blank=True, null=True)
region_of_birth = models.TextField(blank=True,null=True)
def __unicode__(self):
return self.first +" "+ self.last
class Meta:
managed = True
db_table = 'fathers'
verbose_name = 'Father'
verbose_name_plural = 'Fathers'
这应该很简单-但是数据库没有更新
谢谢你!!!整天。。。完全错过了。。。需要补充:
self.helper.form_action = 'dbPostother'
嘎!!!整天。。。完全错过了。。。需要补充:
self.helper.form_action = 'dbPostother'
据推测,该表格无效。您是否在模板中的任何位置显示错误?请添加
其他打印(form.errors.as_data())
并使用结果输出编辑您的答案,请:)尝试以下操作:如果form.is_有效():form.save()其他:打印form.errors
。如果它打印了一个错误记录,您就可以知道那里有什么错误。如下所示:如果您使用{{form}
显示表单,验证错误应该出现在页面上。我认为重写方法中的super
调用应该是第一条语句。该表单可能无效。您是否在模板中的任何位置显示错误?请添加其他打印(form.errors.as_data())
并使用结果输出编辑您的答案,请:)尝试以下操作:如果form.is_有效():form.save()其他:打印form.errors
。如果它打印了一个错误记录,您就可以知道那里有什么错误。如下所示:如果您使用{{form}
显示表单,验证错误应该出现在页面上。我认为重写方法中的super
调用应该是第一条语句。