D
D
Danil Valiulin2014-10-21 07:39:56
Django
Danil Valiulin, 2014-10-21 07:39:56

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)

I recently studied Django and taught from a book in which there is not a single line about CBV, so now I have questions and incomprehensibility as I rewrite the code on CBV.
Question:
I want to make it so that when I click on the URL I have a form, and after submitting the data, the form and the list below it are displayed. But I don't know how to do it, and all I could achieve is that when I go to the URL, a form appears and a list below it, but when I submit it, it gives a 405 error. Please tell me how to do it right.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery, 2014-10-21
@Balalay12

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...

V
Valentine, 2014-10-21
@vvpoloskin

405 - invalid method error. Maybe you should try adding the post method to the view manually and see if it reaches it? and explicitly specify an action for the form?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question