CreateView中的Formset无法保存

时间:2019-01-23 17:14:15

标签: django-class-based-views django-2.1

也许有人可以帮助我?!? 为什么此CreateView无法保存到数据库? 我应该输入更多文本。但是我不知道该话题写什么。如果有人偷看我的代码并给予我帮助,那就太好了。

class BuchungCreateView(CreateView):
    template_name = 'buchung/create_detail.html'
    form_class = BuchungForm

    def get_initial(self):
        trip = get_object_or_404(Trip, id=self.kwargs.get('id'))
        return {
            'trip': trip,
            'preis': trip.preis, 'reiseziel': trip.reiseziel, 'datum_abfahrt': trip.datum_abfahrt
        }
    def get_context_data(self, **kwargs):
        context = super(BuchungCreateView, self).get_context_data(**kwargs)
        context['formset'] = PersonenFormset()
        return context

    def post(self, request, *args, **kwargs):
        formset = PersonenFormset(request.POST)
        if formset.is_valid():
            formset.save()
            return self.form_valid(formset)

    def form_valid(self, formset):
        formset.save()
        return HttpResponseRedirect('/')

0 个答案:

没有答案