Ticket #3639: r6635_create_update_newforms.diff
File r6635_create_update_newforms.diff, 4.9 KB (added by , 17 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 oldforms 4 from django.db.models import FileField 3 from django import newforms as forms 5 4 from django.contrib.auth.views import redirect_to_login 6 5 from django.template import RequestContext 7 6 from django.http import Http404, HttpResponse, HttpResponseRedirect … … 10 9 11 10 def create_object(request, model, template_name=None, 12 11 template_loader=loader, extra_context=None, post_save_redirect=None, 13 login_required=False, follow=None, context_processors=None): 12 login_required=False, context_processors=None, form=forms.BaseForm, 13 formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 14 14 """ 15 15 Generic object-creation function. 16 16 … … 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) 27 if request.POST: 28 # If data was POSTed, we're trying to create a new object 29 new_data = request.POST.copy() 26 ModelForm = forms.form_for_model(model, form=form, formfield_callback=formfield_callback) 27 if request.method == 'POST': 28 form = ModelForm(request.POST, request.FILES) 29 30 if form.is_valid(): 31 new_object = form.save() 30 32 31 if model._meta.has_field_type(FileField):32 new_data.update(request.FILES)33 34 # Check for errors35 errors = manipulator.get_validation_errors(new_data)36 manipulator.do_html2python(new_data)37 38 if not errors:39 # No errors -- this means we can save the data!40 new_object = manipulator.save(new_data)41 42 33 if request.user.is_authenticated(): 43 34 request.user.message_set.create(message=ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name}) 44 35 … … 51 42 else: 52 43 raise ImproperlyConfigured("No URL to redirect to from generic create view.") 53 44 else: 54 # No POST, so we want a brand new form without any data or errors 55 errors = {} 56 new_data = manipulator.flatten_data() 45 form = ModelForm() 57 46 58 # Create the FormWrapper, template, context, response 59 form = oldforms.FormWrapper(manipulator, new_data, errors) 47 # Create the template, context, response 60 48 if not template_name: 61 49 template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) 62 50 t = template_loader.get_template(template_name) … … 73 61 def update_object(request, model, object_id=None, slug=None, 74 62 slug_field='slug', template_name=None, template_loader=loader, 75 63 extra_context=None, post_save_redirect=None, 76 login_required=False, follow=None, context_processors=None, 77 template_object_name='object'): 64 login_required=False, context_processors=None, 65 template_object_name='object', form=forms.BaseForm, 66 formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 78 67 """ 79 68 Generic object-update function. 80 69 … … 102 91 except ObjectDoesNotExist: 103 92 raise Http404, "No %s found for %s" % (model._meta.verbose_name, lookup_kwargs) 104 93 105 manipulator = model.ChangeManipulator(getattr(object, object._meta.pk.attname), follow=follow)94 ModelForm = forms.form_for_instance(object, form=form, formfield_callback=formfield_callback) 106 95 107 if request.POST: 108 new_data = request.POST.copy() 109 if model._meta.has_field_type(FileField): 110 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) 96 if request.method == 'POST': 97 form = ModelForm(request.POST, request.FILES) 98 if form.is_valid(): 99 object = form.save() 115 100 116 101 if request.user.is_authenticated(): 117 102 request.user.message_set.create(message=ugettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name}) … … 124 109 else: 125 110 raise ImproperlyConfigured("No URL to redirect to from generic create view.") 126 111 else: 127 errors = {} 128 # This makes sure the form acurate represents the fields of the place. 129 new_data = manipulator.flatten_data() 112 form = ModelForm() 130 113 131 form = oldforms.FormWrapper(manipulator, new_data, errors)132 114 if not template_name: 133 115 template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) 134 116 t = template_loader.get_template(template_name)