【发布时间】:2014-12-27 21:49:08
【问题描述】:
我正在尝试覆盖CreateView 的get_form() 方法。我的网页有两种相同的形式——一种用于添加“已注册”团队,另一种用于添加“未注册”团队。如果要添加未注册的团队,我想将表单的 team_name 字段设置为“可用”。正如您在下面的代码中看到的那样,我尝试通过覆盖 get_form() 方法来完成此操作
class TeamCreateView(LeagueContextMixin, CreateView):
model = Team
form_class = TeamForm
template_name = "leagueapp/editleague.html"
registered = False # the correct value of registered is passed in urls.py depending on the url that gets hit
# Overwrite the get_success_url() method
def get_success_url(self):
return '/league/editleague/' + self.kwargs.get('league_id')
def get_form(self, form_class):
form_kwargs = self.get_form_kwargs()
if not self.registered:
form_kwargs['team_name'] = "Available"
return form_class(**form_kwargs)
但这给了我错误__init__() got an unexpected keyword argument 'team_name'。我做错了什么和/或有更好的方法来解决这个问题?
编辑: 这是我的团队表格
class TeamForm(ModelForm):
class Meta:
model = Team
fields = ['team_name', 'captain', 'registered', 'team_location', 'phone', 'email', 'team_payment']
widgets = {
'team_name':TextInput(attrs={'class':'form-control input-md'}),
'captain':TextInput(attrs={'class':'form-control input-md'}),
'phone':TextInput(attrs={'class':'form-control input-md'}),
'email':TextInput(attrs={'class':'form-control', 'type':'email'}),
'team_location':TextInput(attrs={'class':'form-control input-md'}),
'team_payment':TextInput(attrs={'class':'form-control'}),
'registered':HiddenInput(),
}
【问题讨论】:
标签: python django django-views