Ticket #12652: create_update.diff

File create_update.diff, 4.3 KB (added by jasongreen, 6 years ago)
  • django/views/generic/create_update.py

     
    9393
    9494def create_object(request, model=None, template_name=None,
    9595        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):
    9797    """
    9898    Generic object-creation function.
    9999
     
    107107        return redirect_to_login(request.path)
    108108
    109109    model, form_class = get_model_and_form_class(model, form_class)
     110    model_name = model._meta.object_name.lower()
    110111    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)
    112119        if form.is_valid():
    113120            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.") % \
    116123                                    {"verbose_name": model._meta.verbose_name}
    117124            messages.success(request, msg, fail_silently=True)
    118125            return redirect(post_save_redirect, new_object)
    119126    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)
    121130
    122131    # Create the template, context, response
    123132    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)
    125134    t = template_loader.get_template(template_name)
    126135    c = RequestContext(request, {
    127136        'form': form,
     
    133142        slug_field='slug', template_name=None, template_loader=loader,
    134143        extra_context=None, post_save_redirect=None, login_required=False,
    135144        context_processors=None, template_object_name='object',
    136         form_class=None):
     145        form_class=None, force_data=None):
    137146    """
    138147    Generic object-update function.
    139148
     
    149158        return redirect_to_login(request.path)
    150159
    151160    model, form_class = get_model_and_form_class(model, form_class)
     161    model_name = model._meta.object_name.lower()
    152162    obj = lookup_object(model, object_id, slug, slug_field)
    153163
    154164    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)
    156172        if form.is_valid():
    157173            obj = form.save()
    158             msg = ugettext("The %(verbose_name)s was updated successfully.") %\
     174            msg = ugettext("The %(verbose_name)s was updated successfully.") % \
    159175                                    {"verbose_name": model._meta.verbose_name}
    160176            messages.success(request, msg, fail_silently=True)
    161177            return redirect(post_save_redirect, obj)
    162178    else:
    163         form = form_class(instance=obj)
     179        form = form_class(instance=obj, prefix=model_name)
    164180
    165181    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)
    167183    t = template_loader.get_template(template_name)
    168184    c = RequestContext(request, {
    169185        'form': form,
     
    198214
    199215    if request.method == 'POST':
    200216        obj.delete()
    201         msg = ugettext("The %(verbose_name)s was deleted.") %\
     217        msg = ugettext("The %(verbose_name)s was deleted.") % \
    202218                                    {"verbose_name": model._meta.verbose_name}
    203219        messages.success(request, msg, fail_silently=True)
    204220        return HttpResponseRedirect(post_delete_redirect)
Back to Top