Ticket #9150: create_update.py.diff

File create_update.py.diff, 2.5 KB (added by Thomas Bechtold, 15 years ago)

Patch:add form_params={} to pass content to forms within generic views

  • django/views/generic/create_update.py

     
    9292
    9393def create_object(request, model=None, template_name=None,
    9494        template_loader=loader, extra_context=None, post_save_redirect=None,
    95         login_required=False, context_processors=None, form_class=None):
     95        login_required=False, context_processors=None, form_class=None, form_params={}):
    9696    """
    9797    Generic object-creation function.
    9898
     
    107107
    108108    model, form_class = get_model_and_form_class(model, form_class)
    109109    if request.method == 'POST':
    110         form = form_class(request.POST, request.FILES)
     110        form = form_class(data=request.POST, files=request.FILES, **form_params)
    111111        if form.is_valid():
    112112            new_object = form.save()
    113113            if request.user.is_authenticated():
    114114                request.user.message_set.create(message=ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name})
    115115            return redirect(post_save_redirect, new_object)
    116116    else:
    117         form = form_class()
     117        form = form_class(**form_params)
    118118
    119119    # Create the template, context, response
    120120    if not template_name:
     
    130130        slug_field='slug', template_name=None, template_loader=loader,
    131131        extra_context=None, post_save_redirect=None, login_required=False,
    132132        context_processors=None, template_object_name='object',
    133         form_class=None):
     133        form_class=None, form_params={}):
    134134    """
    135135    Generic object-update function.
    136136
     
    149149    obj = lookup_object(model, object_id, slug, slug_field)
    150150
    151151    if request.method == 'POST':
    152         form = form_class(request.POST, request.FILES, instance=obj)
     152        form = form_class(data=request.POST, files=request.FILES, instance=obj, **form_params)
    153153        if form.is_valid():
    154154            obj = form.save()
    155155            if request.user.is_authenticated():
    156156                request.user.message_set.create(message=ugettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name})
    157157            return redirect(post_save_redirect, obj)
    158158    else:
    159         form = form_class(instance=obj)
     159        form = form_class(instance=obj, **form_params)
    160160
    161161    if not template_name:
    162162        template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
Back to Top