Ticket #17700: cbv_formsets_doc.diff

File cbv_formsets_doc.diff, 1.4 KB (added by Adrien Lemaire, 13 years ago)

Add an example for using Formsets with class based views

  • docs/topics/forms/formsets.txt

    diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
    index b524c24..4745180 100644
    a b a look at how this might be accomplished:  
    499499            'book_formset': book_formset,
    500500        })
    501501
     502Class-based-views style::
     503
     504    from django.views.generic import UpdateView
     505
     506    class ArticleUpdateView(UpdateView):
     507        """Class-Based View using a FormSet"""
     508
     509        def post(request):
     510            article_formset = ArticleFormSet(request.POST, request.FILES, prefix='articles')
     511            book_formset = BookFormSet(request.POST, request.FILES, prefix='books')
     512            if article_formset.is_valid() and book_formset.is_valid():
     513                # do something with the cleaned_data on the formsets.
     514
     515        def get(request):
     516            article_formset = ArticleFormSet(prefix='articles')
     517            book_formset = BookFormSet(prefix='books')
     518
     519        def render_to_response(context):
     520            super(ArticleUpdateView, self).render_to_response(request,
     521                'manage_articles.html', {
     522                    'article_formset': article_formset,
     523                    'book_formset': book_formset,
     524                })
     525
     526
    502527You would then render the formsets as normal. It is important to point out
    503528that you need to pass ``prefix`` on both the POST and non-POST cases so that
    504529it is rendered and processed correctly.
Back to Top