【发布时间】:2012-05-24 10:39:22
【问题描述】:
我的 models.py 文件底部有一些 ModelForms。一些模型表单正在工作(即它们在模板中正确显示)。
以下是其中的两个:
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()
【问题讨论】:
-
我们需要查看视图和模板。
-
向我们展示分配和扣除类
-
你的 settings.TEMPLATE_DIRS 是什么? “示例模板”的路径是什么?
-
为什么 template_dirs 路径很重要?我对工作的 ModelForm 和非工作的 ModelForm 都使用相同的模板和视图(表单名称除外)...
-
你有两个同名的模板吗?一个在项目/模板中,另一个在项目/应用程序/模板中?您是否尝试在 shell 中实例化表单并调用 form.as_p()?只是为了检查表单是否有效?
标签: django django-templates django-forms