Ticket #3639: create_update__newforms.diff
File create_update__newforms.diff, 3.5 KB (added by , 18 years ago) |
---|
-
django/views/generic/create_update.py
1 1 from django.core.xheaders import populate_xheaders 2 2 from django.template import loader 3 from django import oldforms3 from django import newforms as forms 4 4 from django.db.models import FileField 5 5 from django.contrib.auth.views import redirect_to_login 6 6 from django.template import RequestContext … … 23 23 if login_required and not request.user.is_authenticated(): 24 24 return redirect_to_login(request.path) 25 25 26 manipulator = model.AddManipulator(follow=follow)26 ModelForm=forms.models.form_from_model(model) 27 27 if request.POST: 28 28 # If data was POSTed, we're trying to create a new object 29 29 new_data = request.POST.copy() … … 31 31 if model._meta.has_field_type(FileField): 32 32 new_data.update(request.FILES) 33 33 34 # Check for errors 35 errors = manipulator.get_validation_errors(new_data) 36 manipulator.do_html2python(new_data) 34 # Actually read the data 35 form=ModelForm(new_data) 37 36 38 if not errors:37 if not form.errors: 39 38 # No errors -- this means we can save the data! 40 new_object = manipulator.save(new_data)39 new_object = form.save() 41 40 42 41 if request.user.is_authenticated(): 43 42 request.user.message_set.create(message=gettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name}) … … 53 52 else: 54 53 # No POST, so we want a brand new form without any data or errors 55 54 errors = {} 56 new_data = manipulator.flatten_data()55 form=ModelForm() 57 56 58 # Create the FormWrapper, template, context, response 59 form = oldforms.FormWrapper(manipulator, new_data, errors) 57 # Create the template, context, response 60 58 if not template_name: 61 59 template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) 62 60 t = template_loader.get_template(template_name) … … 102 100 except ObjectDoesNotExist: 103 101 raise Http404, "No %s found for %s" % (model._meta.verbose_name, lookup_kwargs) 104 102 105 manipulator = model.ChangeManipulator(getattr(object, object._meta.pk.attname), follow=follow)103 ModelForm = forms.models.form_for_instance(object) 106 104 107 105 if request.POST: 108 106 new_data = request.POST.copy() 109 107 if model._meta.has_field_type(FileField): 110 108 new_data.update(request.FILES) 111 errors = manipulator.get_validation_errors(new_data) 112 manipulator.do_html2python(new_data) 113 if not errors: 114 object = manipulator.save(new_data) 109 form=ModelForm(new_data) 110 if not form.errors: 111 object = form.save(new_data) 115 112 116 113 if request.user.is_authenticated(): 117 114 request.user.message_set.create(message=gettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name}) … … 126 123 else: 127 124 errors = {} 128 125 # This makes sure the form acurate represents the fields of the place. 129 new_data = manipulator.flatten_data()126 form = ModelForm() 130 127 131 form = oldforms.FormWrapper(manipulator, new_data, errors)132 128 if not template_name: 133 129 template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) 134 130 t = template_loader.get_template(template_name)