[Solved]-Django-'NoneType' object is not callable

19๐Ÿ‘

โœ…

I think this is your issue: You are using a FormView but havenโ€™t defined a form class to use. Either set a form_class attr on the class, or override the get_form_class method:

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:

def form_valid(self, form, **kwargs):
    form = InputForm(self.request.POST)  # <- remove this line
    if form.is_valid():
    ...
๐Ÿ‘คArsh Singh

2๐Ÿ‘

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:

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})
๐Ÿ‘คPaul

0๐Ÿ‘

Chances are that the problem is with your forms.py file. 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. For example if your model is lets suppose Post,
It should be defined as Post instead of Post()

๐Ÿ‘คPritam Banik

Leave a comment