【发布时间】:2015-02-12 23:26:38
【问题描述】:
我试图避免在表单和(基于类的)视图中重复字段列表和模型说明符。
This answer 建议定义一个包含字段列表的“元类”,并在表单和视图中继承该类。
它对表单工作正常,但是将列表和目标模型继承到视图中的以下代码会导致此错误:
TemplateResponseMixin 需要“template_name”的定义或“get_template_names()”的实现
我不知道这种变化是如何导致该错误的。
forms.py:
class ScenarioFormInfo:
model = Scenario
fields = ['scenario_name', 'description', 'game_type',
'scenario_size', 'weather', 'battle_type', 'attacker',
'suitable_for_axis_vs_AI', 'suitable_for_allies_vs_AI',
'playtested_H2H', 'suitable_for_H2H',
'scenario_file', 'preview']
class ScenarioForm(forms.ModelForm):
Meta = ScenarioFormInfo
views.py:
class ScenarioUpload(generic.CreateView, forms.ScenarioFormInfo):
form_class = ScenarioForm
# model = Scenario
# fields = ['scenario_name', 'description', 'game_type',
# 'scenario_size', 'weather', 'battle_type', 'attacker',
# 'suitable_for_axis_vs_AI', 'suitable_for_allies_vs_AI',
# 'suitable_for_H2H', 'playtested_H2H',
# 'scenario_file', 'preview']
【问题讨论】:
-
我刚刚发现这是模型说明符,而不是字段列表,这是问题所在。继承字段列表工作正常,但视图似乎需要直接在其中声明的模型说明符。这很奇怪吗?
标签: django django-views