无法保存表单

时间:2015-11-02 10:34:24

标签: python django forms

我遇到问题,即在保存提交按钮表单后我无法保存表单。我只能在管理信息中心执行此操作。

class EventPage(CreateView):
    template_name = 'event.html'
    form_class = RegisterToEvent
    second_form_class = UploadDocument

    def form_valid(self, form, **kwargs):
        form.instance.event = Event.objects.get(id = self.kwargs['event_id'])
        form.save()
        return super(EventPage, self).form_valid(form)

    def form_invalid(self, **kwargs):
        return self.render_to_response(self.get_context_data(**kwargs))

    def get_context_data(self, **kwargs):
        context = super(EventPage, self).get_context_data(**kwargs)
        event = Event.objects.get(id = self.kwargs['event_id'])
        participants = Participant.objects.filter(event_id = event.id)
        context['participants'] = participants
        context['event'] = event
        if 'RegisterToEvent' not in context:
            context['RegisterToEvent'] = self.form_class
        if 'UploadDocument' not in context:
            context['UploadDocument'] = self.second_form_class
        return context

    def get_object(self):
        return get_object_or_404(Event, pk=self.kwargs['event_id'])

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        if 'register_form' in request.POST:
            form_class = self.get_form_class()
            form_name = 'RegisterToEvent'
        else:
            form_class = self.second_form_class
            form_name = 'UploadDocument'
        form = self.get_form(form_class)
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(**{form_name: form})

我有两种形式:RegisterToEvent和UploadDocuments。问题是当我试图将Register保存到事件表单时。我没有收到任何错误,只有

  

[02 / Nov / 2015 10:29:45]" POST / event-2 / HTTP / 1.1" 302 0

urls.py

urlpatterns = [
    url(r'^$', 'event.views.home', name='home'),
    url(r'^about/', 'meetingsSystem.views.about', name='about'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('registration.backends.default.urls')),
    url(r'^create-event/', 'event.views.create_event', name='create_event'),
    url(r'^event-(?P<event_id>[0-9]+)/$', EventPage.as_view()),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

0 个答案:

没有答案