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():
...
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})
- Django REST serializer: create object without saving
- Django Raw SQL give me TypeError not enough arguments
- Dict keys with spaces in Django templates
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()