Python django.views.generic.edit 模块,ModelFormMixin() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用django.views.generic.edit.ModelFormMixin()

项目:hydra    作者:Our-Revolution    | 项目源码 | 文件源码
def form_valid(self, form):      
        try:
            self.object = form.save()
            messages.add_message(self.request, messages.SUCCESS, "Your event has been created.")
            return super(ModelFormMixin, self).form_valid(form)

        except BaseException, e:
            if hasattr(e, '__iter__'):
                for exc in e:
                    messages.add_message(self.request, messages.ERROR, "Error creating your event -- %s" % exc[1][0])
                    if hasattr(form, exc[0]):
                        form.add_error(exc[0], exc[1][0])
            elif isinstance(e, UnicodeError):
                field =  e[1][e[1][0:e[3]].rfind('&')+1:e[1][0:e[3]].rfind('=')]
                messages.add_message(self.request, messages.ERROR, "Error creating your event -- You had some special characters in your %s field, please remove those and try again." % field)
                if field in form.fields:
                    form.add_error(field, "Please remove special characters and try again.")
            return super(EventCreate, self).form_invalid(form)
项目:hydra    作者:Our-Revolution    | 项目源码 | 文件源码
def form_valid(self, form):
        try:
            self.object = form.save()
            messages.add_message(self.request, messages.SUCCESS, "Your event has been updated.")
            return super(ModelFormMixin, self).form_valid(form)

        except BaseException, e:
            if hasattr(e, '__iter__'):
                for exc in e:
                    messages.add_message(self.request, messages.ERROR, "Error creating your event -- %s" % exc[1][0])
                    if hasattr(form, exc[0]):
                        form.add_error(exc[0], exc[1][0])
            elif isinstance(e, UnicodeError):
                field =  e[1][e[1][0:e[3]].rfind('&')+1:e[1][0:e[3]].rfind('=')]
                if field in form.fields:
                    form.add_error(field, "Please remove special characters and try again.")
                messages.add_message(self.request, messages.ERROR, "Error updating your event -- You had some special characters in your %s field, please remove those and try again." % field)
            return super(EventEdit, self).form_invalid(form)