简体   繁体   English

django-'NoneType' 对象不可调用

[英]django-'NoneType' object is not callable

I got this error, when redirecting to the result page.重定向到结果页面时出现此错误。 Is that because in the redirected page, the "POST.get" function cannot get what the user has entered in the form before redirecting?那是不是因为在重定向页面中,“POST.get”函数在重定向前无法获取用户在表单中输入的内容?

views.py视图.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()

Traceback:追溯:

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

Inputform输入表单

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)

For the same function, I tried another code, even though without error but cannot display the form data, it displays as none.对于相同的功能,我尝试了另一个代码,即使没有错误但无法显示表单数据,它也显示为无。 Hope you can answer my question in the following link:希望您能在以下链接中回答我的问题:

django- why after redirecting, the form display "None" django-为什么重定向后,表单显示“无”

I think this is your issue: You are using a FormView but haven't defined a form class to use.我认为这是您的问题:您正在使用FormView但尚未定义要使用的表单类。 Either set a form_class attr on the class, or override the get_form_class method:要么在类上设置一个form_class属性,要么覆盖get_form_class方法:

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

Also, the form_valid method will receive the form instance, you don't need to instantiate it manually:此外, form_valid方法将接收表单实例,您不需要手动实例化它:

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

Lets make another thread.让我们再做一个线程。 :) The only way you can get "return outside function" error - you are trying to return something not from function. :) 获得“返回函数外”错误的唯一方法 - 您试图返回不是从函数中返回的内容。 :) It could happen usually because of misstype or wrong indentation. :) 这通常是由于错误类型或错误的缩进而发生的。 Could you please provide the code where you get this error?您能否提供出现此错误的代码? I believe that there is something wrong with indentation there.我相信那里的缩进有问题。

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

    if form.is_valid(): # <- it is not a function or method
                        #  it is a class declaration  
        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): #<- and this is a method
        #... all your following code

I'm not familiar with Django FormViews, but looks like correct code could be like this:我不熟悉 Django FormViews,但看起来正确的代码可能是这样的:

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

    def form_valid(self, form):
        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})

Chances are that the problem is with your forms.py file.问题很可能出在您的 forms.py 文件上。 When you are importing and using the model in your forms.py file you are using parenthesis to define your model which you should not.当您在 forms.py 文件中导入和使用模型时,您正在使用括号来定义您不应该使用的模型。 For example if your model is lets suppose Post, It should be defined as Post instead of Post()例如,如果您的模型是让我们假设 Post,则应将其定义为Post而不是Post()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM