注:已解决,最后解决。
我正在尝试将请求值传递给表单我必须过滤表单中的字段,但它一直返回 TypeError,我认为代码没问题。
视图.py:
class AddProposalSet(FormView):
"""
Create a new prpoposal set, it can be related to a debate or be in free mode,
which is not linked to anything. If it's linked to a debate, people can
make their proposals related to the debate notes.
.. versionadded: 0.1.5
:rtype: Form object
:context: form, get_place
"""
#form_class = ProposalSetForm
template_name = 'proposals/proposalset_form.html'
def get_success_url(self):
space = self.kwargs['space_url']
return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space})
def get_form_class(self):
"""
The reason for overriding "form_class" is that we pass here the
request to the form, that way we can filter the debates based on the
current space.
"""
return ProposalSetForm(self.kwargs['space_url'])
def form_valid(self, form):
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
form_uncommited = form.save(commit=False)
form_uncommited.space = self.space
form_uncommited.author = self.request.user
form_uncommited.save()
return super(AddProposalSet, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(AddProposalSet, self).get_context_data(**kwargs)
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
context['get_place'] = self.space
return context
@method_decorator(permission_required('proposals.add_proposalset'))
def dispatch(self, *args, **kwargs):
return super(AddProposalSet, self).dispatch(*args, **kwargs)
表格.py
class ProposalSetForm(ModelForm):
"""
ProposalSetForm is a basic form autogenerated form from ProposalSet model.
Returns an empty form to create a new proposal set.
:rtype: HTML Form
.. versionadded:: 0.1.5b
"""
class Meta:
model = ProposalSet
def __init__(self, *args, **kwargs):
super(ProposalSetForm, self).__init__(*args, **kwargs)
get_place = get_object_or_404(Space, url=args[0])
if self.instance:
self.fields['debate'].queryset = Debate.objects.filter(end_date__lte=datetime.date.today(), space=get_place)
错误:
TypeError at /en-gb/spaces/test/proposal/add/set/
'ProposalSetForm' object is not callable
我无法找到可能发生的事情,我错过了什么吗?
解决方案:
视图.py
class AddProposalSet(FormView):
"""
Create a new prpoposal set, it can be related to a debate or be in free mode,
which is not linked to anything. If it's linked to a debate, people can
make their proposals related to the debate notes.
.. versionadded: 0.1.5
:rtype: Form object
:context: form, get_place
"""
form_class = ProposalSetForm
template_name = 'proposals/proposalset_form.html'
def get_success_url(self):
space = self.kwargs['space_url']
return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space})
def get_form_kwargs(self, **kwargs):
kwargs = super(AddProposalSet, self).get_form_kwargs(**kwargs)
kwargs['initial']['space'] = self.kwargs['space_url']
return kwargs
def form_valid(self, form):
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
form_uncommited = form.save(commit=False)
form_uncommited.space = self.space
form_uncommited.author = self.request.user
form_uncommited.save()
return super(AddProposalSet, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(AddProposalSet, self).get_context_data(**kwargs)
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
context['get_place'] = self.space
return context
@method_decorator(permission_required('proposals.add_proposalset'))
def dispatch(self, *args, **kwargs):
return super(AddProposalSet, self).dispatch(*args, **kwargs)
表格.py
class ProposalSetForm(ModelForm):
"""
ProposalSetForm is a basic form autogenerated form from ProposalSet model.
Returns an empty form to create a new proposal set.
:rtype: HTML Form
.. versionadded:: 0.1.5b
"""
class Meta:
model = ProposalSet
def __init__(self, *args, **kwargs):
super(ProposalSetForm, self).__init__(*args, **kwargs)
get_place = get_object_or_404(Space, url=kwargs['initial']['space'])
if self.instance:
self.fields['debate'].queryset = Debate.objects.filter(end_date__lte=datetime.date.today(), space=get_place)
print self.fields['debate']