2012-05-16 82 views
1

我在models.py文件的底部有幾個ModelForms。某些模型表單正在工作(即它們正確顯示在模板中)。Django ModelForm不在模板中顯示

這些都是幾個2的工作:

class Account_Balance_Form(ModelForm): 
    class Meta: 
     model = Account_Balance 
     fields = ('date','balance') 

class Asset_Form(ModelForm): 
    class Meta: 
     model = Asset 
     exclude = ('account','id') 

別人不工作,雖然。即使使用相同的視圖(傳遞不同的ModelForm)到相同的模板。這是2不工作:

class Allocation_Form(ModelForm): 
    class Meta: 
     model = Allocation 

class Deduction_Form(ModelForm): 
    class Meta: 
     model = Deduction 

不是真的知道我做錯了......我想跑執行syncdb,但這並沒有幫助。此外,它看起來像表單對象正在創建罰款:

allocation_form <forecast.models.Allocation_Form object at 0x90f15ac> 

他們只是沒有顯示......任何想法?

===================

僅供參考,樣品認爲工作:

def view_allocation(request): 
    form = Asset_Form() 
    return render_to_response('alloc.html', 
           {'form': form}) 

不起作用:

def view_allocation(request): 
    form = Allocation_Form() 
    return render_to_response('alloc.html', 
           {'form': form}) 

樣模板:

<html> 
<body> 
{{ form.as_p }} 
</body> 
</html> 

的要求:

class Allocation(models.Model): 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=40) 
    account = models.ForeignKey(Account) 
    amount = models.DecimalField(max_digits=20,decimal_places=2) 
    percent = models.DecimalField(max_digits=10,decimal_places=10) 
    allocation_group = models.IntegerField(max_length=11) 
    def __unicode__(self): 
     return self.name 


class Deduction(models.Model): 
    iei = models.ForeignKey(Inc_Exp_Item, null=True, blank=True) 
    name = models.CharField(max_length=40) 
    amount = models.DecimalField(max_digits=20,decimal_places=2) 
    percent = models.DecimalField(max_digits=10,decimal_places=10) 
    before_tax = models.BooleanField() 
    credit_debit = models.CharField(max_length=6, 
            choices=(('Debit','Income'), 
              ('Credit','Expense'),)) 
    tax_category = models.ForeignKey(Tax_Category) 
    account = models.ForeignKey(Account) 
    active = models.BooleanField() 
    deduct_taxes = models.BooleanField() 
+2

我們需要查看視圖和模板。 –

+2

向我們顯示分配和扣除類 – szaman

+0

什麼是您的設置.TEMPLATE_DIRS? 「Sample Template」的路徑是什麼? – jpic

回答

2

感謝您的幫助大家,ESP。啤酒。試圖在shell中打印表單並得到一個類型錯誤。

該對象正在創建,但無法打印(或.as_p())。

的問題是在賬戶模式,分配和演繹了一個外鍵:

class Account(models.Model): 
    user = models.ForeignKey(User) 
    account_type = models.ForeignKey(Account_Type) 
    def __unicode__(self): 
     return self.account_type 

我刪除了Unicode的方法和它的工作。我猜unicode不會返回另一個模型的unicode方法,哈哈。