2015-10-24 26 views
7

結果ページにリダイレクトすると、このエラーが発生しました。これは、リダイレクトされたページでは、 "POST.get"関数はユーザーがリダイレクトする前にフォームに入力した内容を取得できないためですか?django-'NoneType 'オブジェクトは呼び出し可能ではありません

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 

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) 

、私はエラーなしにフォームデータを表示することはできませんが、別のコードを試しましたが、それはnoneとして表示されます。あなたは以下のリンクで私の質問に答えることを願って:

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

+1

は、あなたがより多くのトレースバックしたり、コードの行が失敗した点を提供していただけますか? – Paul

+0

こんにちは@Paul、それはどの行が間違っているかを指摘していません。私はすべてのトレースバックを実行しました –

+0

どのテキストエディタまたはIDEを使用しますか? – Paul

答えて

8

私はこれがあなたの問題だと思う:あなたはFormViewを使用しているが、使用するフォームクラスを定義していません。どちらのクラスにform_classのattrを設定、または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(): 
    ... 
+0

こんにちは@Arshシン、あなたは間違っていると思いますか? "私のviews.pyにrender_to_response({" object_list ":queryset})を返します。 –

+0

@Elena今、別のエラーが発生しましたか? – Paul

+0

@Eleena私はあなたの2番目の質問があなたが今得ているエラーとリンクしていると信じています。あなたは今何を得ているのですか? "「:(result_list.html '{ '形フォーム'} – Paul

2

別のスレッドを作れます。 :) 「関数外に戻す」エラーを得る唯一の方法 - 関数からではなく何かを返そうとしています。 :)これは通常、誤ったタイプや間違ったインデントのために発生する可能性があります。 このエラーが発生した場所のコードを入力してください。私はそこにインデントの問題があると信じています。

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 

私はこのような可能性がありジャンゴ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}) 
+0

私はviews.pyを更新しました、私はかなりそれが原因インデントの問題であるとは思わない。エラーが(リクエスト、「result_listをレンダリングリターン」の行で発生します。 HTML」、{ 『形』:フォーム})」 –

+0

@Elenaあなたからのコードが正しい疑問がある場合は、関数からではない返却しようとしているしかし、あなたは正しいです - それはインデント問題ではありません:)私は私の答えを更新しました。再び。 – Paul

+0

私はあなたが提案するようなコードを持っていたが、@Arshシンの回答abov eは "FormViewのdef form_valid(self、form)を削除するので、削除しました。 –

関連する問題