2017-02-10 49 views
1

我正在与一些应该很简单的事情作斗争。事实上,我之前做过这件事,而且我似乎无法理解发生了什么。Django 1.10模型保存问题

所有我想要做的是挽救我的前端的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','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) 

urls.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 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' 

这应该是简单的 - 但数据库没有更新......

感谢

+0

大概,表单无效。您是否在模板中的任何位置显示错误? –

+0

加入一个'else print(form.errors.as_data())'并编辑你的答案和结果输出,请:) – fixmycode

+0

试试这个: 'if form.is_valid(): form.save() else: print form.errors'。如果它打印出一个错误的字典,你可以知道有什么错误。 –

回答

0

GAH!整天...完全错过...需要补充:

self.helper.form_action = 'dbPostother'