gpt4 book ai didi

python - django -'NoneType' 对象不可调用

转载 作者:太空狗 更新时间:2023-10-29 20:18:23 24 4
gpt4 key购买 nike

我在重定向到结果页面时遇到了这个错误。是因为在重定向页面中,“POST.get”函数无法获取用户在重定向前在表单中输入的内容吗?

views.py

class InputFormView(FormView):
template_name = 'inputform.html'
form_class = InputForm

def get_success_url(self):
return ''.join(
[
reverse('result'),
'?company=',self.request.POST.get('company'),
'&region=',self.request.POST.get('region') <is there anything wrong here---?
]
)


class ReulstView(FormView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result

if form.is_valid():
company = form.cleaned_data['company']
region = form.cleaned_data['region']

self.queryset=Result.objects.filter(region=region)

return render(request, 'result_list.html', {'form': form})

def get_context_data(self, **kwargs):
context = super(ReulstView, self).get_context_data(**kwargs)
context["sales"] = self.get_queryset().aggregate(Sum('sales'))

context["company"] = self.request.POST.get("company")
context["region"] = self.request.POST.get("region")

return context

def get_queryset(self):
return Result.objects.all()

回溯:

File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\edit.py" in get
205. form = self.get_form()
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\edit.py" in get_form
74. return form_class(**self.get_form_kwargs())

Exception Type: TypeError at /result_list/
Exception Value: 'NoneType' object is not callable

输入表单

class InputForm(forms.ModelForm):
company=forms.CharField(widget=forms.TextInput, label="Company",error_messages={'required': 'Please enter the company name'},required=True)

#region This form can display correctly with drop down list
iquery = Dupont.objects.values_list('region', flat=True).distinct()
iquery_choices = [('', 'None')] + [(region,region) for region in iquery]
region = forms.ChoiceField(choices=iquery_choices)

同样的功能,我尝试了另外一段代码,虽然没有报错但是无法显示表单数据,显示为none。希望您能在以下链接中回答我的问题:

django- why after redirecting, the form display "None"

最佳答案

我认为这是您的问题:您正在使用 FormView 但尚未定义要使用的表单类。在类上设置 form_class 属性,或覆盖 get_form_class 方法:

class ReulstView(FormView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result
form_class = InputForm

此外,form_valid 方法将接收表单实例,您无需手动实例化它:

def form_valid(self, form, **kwargs):
form = InputForm(self.request.POST) # <- remove this line
if form.is_valid():
...

关于python - django -'NoneType' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33315358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com