Невозможно получить объект класса формы в представлении шаблона
Я хочу получить объект формы из self.Form
Это моя форма
class ActionLogSearchForm(forms.Form):
key_words = forms.CharField(required=False)
и я установил форму как form_class, однако я не могу получить данные формы в view
class ActionLogListView(LoginRequiredMixin, ListSearchView):
template_name = "message_logs/action_log.html"
form_class = ActionLogSearchForm
def get_queryset(self):
res = []
form = self.form ## somehow form is None
print(form.cleaned_data) # error occurs here. 'NoneType' object has no attribute 'cleaned_data'
Я думаю, что это самый простой набор, но как я могу заставить его работать?
Попробуйте это:
from django.http import HttpResponseRedirect
from django.shortcuts import render
class ActionLogSearchForm(forms.Form):
key_words = forms.CharField(required=False)
class ActionLogListView(LoginRequiredMixin, ListSearchView, request):
form_class = ActionLogSearchForm
def get_queryset(self, request):
res = []
if request.method == 'POST':
form = self.form(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'message_logs/action_log.html', {'form': form})