Answer the question
In order to leave comments, you need to log in
How to use CBV with form in Django?
Hello! I have code:
class OperationsList(object):
model = Many
earn = False
cost = False
title = ''
summa = {}
form = ''
def get_queryset(self):
qs = Many.objects.filter(users=self.request.user.id).order_by('-date')
return qs
def get_context_data(self, **kwargs):
context = super(OperationsList, self).get_context_data(**kwargs)
context['user_username'] = self.request.user.username
context['e'] = self.earn
context['c'] = self.cost
context['title'] = self.title
if self.form:
context['form'] = self.form
if self.summa['sum__sum'] is None:
context['summa'] = 0
else:
context['summa'] = self.summa['sum__sum']
return context
class EarnPeriods(OperationsList, FormMixin, ListView):
form_class = forms.DateForm
form = forms.DateForm
template_name = 'period.html'
context_object_name = 'earnings_list'
earn = True
now_date = datetime.date.today()
date_start = now_date
date_end = now_date
def form_valid(self, form):
self.date_start = form.clean['date_start']
self.date_end = form.clean['date_end']
return super(EarnPeriods, self).form_valid(form)
def get_queryset(self):
print(self.date_start)
print(self.date_end)
self.summa = super(EarnPeriods, self).get_queryset().filter(date__range=(self.date_start, self.date_end), sum__gt=0).aggregate(Sum('sum'))
return super(EarnPeriods, self).get_queryset().filter(date__range=(self.date_start, self.date_end), sum__gt=0)
Answer the question
In order to leave comments, you need to log in
In the EarnPeriods class, you use inheritance from the ListView class, which does not support post requests https://docs.djangoproject.com/en/dev/ref/class-ba...
This class is only used to display a "list of objects".
And you send the form on the html-page with a post-request. Here is the 405 error.
In general, you have 3 options:
1) use a class that supports post requests. Any of these https://docs.djangoproject.com/en/dev/ref/class-ba... depending on what you need;
2) send the form in the template with a get request (only if this form receives and displays objects, and does not change them in the database);
3) write the methods you need yourself (including post()), but then it’s better to inherit from some common class like https://docs.djangoproject.com/en/dev/ref/class-ba...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question