F
F
Fedor_PV2018-08-03 10:08:25
Django
Fedor_PV, 2018-08-03 10:08:25

How to combine CreateView and ListView on one page?

Guru Dzhangi, tell me, pliz, to a stupid newbie on the following question -
There is a CreateView, it creates a new message.
There is a ListView, it displays a list of existing messages.
But how to make it possible to write a new message (CreateView) like in any correspondence, and at the same time, on the same page, all past correspondence (a list of all early messages via ListView) is visible from below?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aerobelov, 2018-08-03
@Fedor_PV

I’m recently learning Django, I may be wrong, but in my opinion, you need to request the messages that you need in the get-part of Create-view and pass them in the context to the page template, and then format them as you want.

V
Vladimir, 2018-08-03
@FonVald

I had a similar issue not too long ago. As I understand it, the decision comes down to inheritance from FormMixin and ListView with post method predefined
Here is an example of my implementation but using DetailView

class PartnerOrderView(FormMixin, DetailView):
    template_name = 'order-partner.html'
    form_class = PartnerBarcodeOrderForm
    model = Partner

    def post(self, request, *args, **kwargs):
        """ Обработка POST при использовани FormMixin в DetailView """
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question