I want to get the form object from self.Form
This is my form
JavaScript
x
3
1
class ActionLogSearchForm(forms.Form):
2
key_words = forms.CharField(required=False)
3
and I set form as form_class, however I can’t fetch the form data in view
JavaScript
1
8
1
class ActionLogListView(LoginRequiredMixin, ListSearchView):
2
template_name = "message_logs/action_log.html"
3
form_class = ActionLogSearchForm
4
def get_queryset(self):
5
res = []
6
form = self.form ## somehow form is None
7
print(form.cleaned_data) # error occurs here. 'NoneType' object has no attribute 'cleaned_data'
8
I think this is the simplest set, but how can I make it work?
Advertisement
Answer
Try this:
JavaScript
1
19
19
1
from django.http import HttpResponseRedirect
2
from django.shortcuts import render
3
4
class ActionLogSearchForm(forms.Form):
5
key_words = forms.CharField(required=False)
6
7
class ActionLogListView(LoginRequiredMixin, ListSearchView, request):
8
form_class = ActionLogSearchForm
9
def get_queryset(self, request):
10
res = []
11
if request.method == 'POST':
12
form = self.form(request.POST)
13
if form.is_valid():
14
return HttpResponseRedirect('/thanks/')
15
else:
16
form = NameForm()
17
18
return render(request, 'message_logs/action_log.html', {'form': form})
19