簡體   English   中英

django-'NoneType' 對象不可調用

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

重定向到結果頁面時出現此錯誤。 那是不是因為在重定向頁面中,“POST.get”函數在重定向前無法獲取用戶在表單中輸入的內容?

視圖.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)

對於相同的功能,我嘗試了另一個代碼,即使沒有錯誤但無法顯示表單數據,它也顯示為無。 希望您能在以下鏈接中回答我的問題:

django-為什么重定向后,表單顯示“無”

我認為這是您的問題:您正在使用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():
    ...

讓我們再做一個線程。 :) 獲得“返回函數外”錯誤的唯一方法 - 您試圖返回不是從函數中返回的內容。 :) 這通常是由於錯誤類型或錯誤的縮進而發生的。 您能否提供出現此錯誤的代碼? 我相信那里的縮進有問題。

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

我不熟悉 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})

問題很可能出在您的 forms.py 文件上。 當您在 forms.py 文件中導入和使用模型時,您正在使用括號來定義您不應該使用的模型。 例如,如果您的模型是讓我們假設 Post,則應將其定義為Post而不是Post()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM