Ticket #12652: create_update.diff
File create_update.diff, 4.3 KB (added by , 15 years ago) |
---|
-
django/views/generic/create_update.py
93 93 94 94 def create_object(request, model=None, template_name=None, 95 95 template_loader=loader, extra_context=None, post_save_redirect=None, 96 login_required=False, context_processors=None, form_class=None ):96 login_required=False, context_processors=None, form_class=None, initial=None, force_data=None): 97 97 """ 98 98 Generic object-creation function. 99 99 … … 107 107 return redirect_to_login(request.path) 108 108 109 109 model, form_class = get_model_and_form_class(model, form_class) 110 model_name = model._meta.object_name.lower() 110 111 if request.method == 'POST': 111 form = form_class(request.POST, request.FILES) 112 if(force_data): 113 data = {} 114 data.update(request.POST) 115 data.update(force_data) 116 else: 117 data = request.POST 118 form = form_class(data, request.FILES, prefix=model_name) 112 119 if form.is_valid(): 113 120 new_object = form.save() 114 115 msg = ugettext("The %(verbose_name)s was created successfully.") % \121 122 msg = ugettext("The %(verbose_name)s was created successfully.") % \ 116 123 {"verbose_name": model._meta.verbose_name} 117 124 messages.success(request, msg, fail_silently=True) 118 125 return redirect(post_save_redirect, new_object) 119 126 else: 120 form = form_class() 127 data = dict([(k, request.GET[k]) for k in request.GET.keys()]) 128 data.update(initial or {}) 129 form = form_class(initial=data, prefix=model_name) 121 130 122 131 # Create the template, context, response 123 132 if not template_name: 124 template_name = "%s/%s_form.html" % (model._meta.app_label, model ._meta.object_name.lower())133 template_name = "%s/%s_form.html" % (model._meta.app_label, model_name) 125 134 t = template_loader.get_template(template_name) 126 135 c = RequestContext(request, { 127 136 'form': form, … … 133 142 slug_field='slug', template_name=None, template_loader=loader, 134 143 extra_context=None, post_save_redirect=None, login_required=False, 135 144 context_processors=None, template_object_name='object', 136 form_class=None ):145 form_class=None, force_data=None): 137 146 """ 138 147 Generic object-update function. 139 148 … … 149 158 return redirect_to_login(request.path) 150 159 151 160 model, form_class = get_model_and_form_class(model, form_class) 161 model_name = model._meta.object_name.lower() 152 162 obj = lookup_object(model, object_id, slug, slug_field) 153 163 154 164 if request.method == 'POST': 155 form = form_class(request.POST, request.FILES, instance=obj) 165 if(force_data): 166 data = {} 167 data.update(request.POST) 168 data.update(force_data) 169 else: 170 data = request.POST 171 form = form_class(data, request.FILES, instance=obj, prefix=model_name) 156 172 if form.is_valid(): 157 173 obj = form.save() 158 msg = ugettext("The %(verbose_name)s was updated successfully.") % \174 msg = ugettext("The %(verbose_name)s was updated successfully.") % \ 159 175 {"verbose_name": model._meta.verbose_name} 160 176 messages.success(request, msg, fail_silently=True) 161 177 return redirect(post_save_redirect, obj) 162 178 else: 163 form = form_class(instance=obj )179 form = form_class(instance=obj, prefix=model_name) 164 180 165 181 if not template_name: 166 template_name = "%s/%s_form.html" % (model._meta.app_label, model ._meta.object_name.lower())182 template_name = "%s/%s_form.html" % (model._meta.app_label, model_name) 167 183 t = template_loader.get_template(template_name) 168 184 c = RequestContext(request, { 169 185 'form': form, … … 198 214 199 215 if request.method == 'POST': 200 216 obj.delete() 201 msg = ugettext("The %(verbose_name)s was deleted.") % \217 msg = ugettext("The %(verbose_name)s was deleted.") % \ 202 218 {"verbose_name": model._meta.verbose_name} 203 219 messages.success(request, msg, fail_silently=True) 204 220 return HttpResponseRedirect(post_delete_redirect)